Quantcast
Channel: Unix Scripting – wikiconsole
Viewing all articles
Browse latest Browse all 10

Unix Shell Scripts – Example 1

$
0
0

Write a shell script sum.sh that takes an unspecified number of command line arguments (up to 9) of ints and finds their sum. Modify the code to add a number to the sum only if the number is greater than 10

Ans:

#! /bin/sh sum=0

for var in “$@” do

if [fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][ $var –gt 10 ]
   then

sum=`expr $sum + $var`
   fi

done
printf "%sn" $sum

% sh example1.sh  2  4  5     –> run the script as

[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]


Viewing all articles
Browse latest Browse all 10

Trending Articles