├── shell1.sh ├── forloop.sh ├── for2.sh ├── sq_area.sh ├── while2.sh ├── var1.sh ├── array_add.sh ├── while.sh ├── SI.sh ├── compare_strings.sh ├── leap_yr.sh ├── fibo.sh ├── escape.sh ├── func2.sh ├── func4.sh ├── variables.sh ├── sum_of_digits.sh ├── pos_neg.sh ├── factorial.sh ├── recursion.sh ├── README.md ├── func3.sh ├── armstrong.sh ├── while3.sh ├── palin.sh ├── max.sh ├── calculator.sh ├── func1.sh ├── salary.sh └── func5.sh /shell1.sh: -------------------------------------------------------------------------------- 1 | echo "What is your name?" 2 | read PERSON 3 | echo "Hello, $PERSON" -------------------------------------------------------------------------------- /forloop.sh: -------------------------------------------------------------------------------- 1 | for i in 1 2 3 4 5 2 | do 3 | echo "Looping ... number $i" 4 | done -------------------------------------------------------------------------------- /for2.sh: -------------------------------------------------------------------------------- 1 | for i in hello 1 * 2 goodbye 2 | do 3 | echo "Looping ... i is set to $i" 4 | done -------------------------------------------------------------------------------- /sq_area.sh: -------------------------------------------------------------------------------- 1 | echo "Enter side of a Square:" 2 | read s 3 | echo "Area of Square: ` expr $s \* $s `" -------------------------------------------------------------------------------- /while2.sh: -------------------------------------------------------------------------------- 1 | while : 2 | do 3 | echo "Please type something in (^C to quit)" 4 | read INPUT_STRING 5 | echo "You typed: $INPUT_STRING" 6 | done -------------------------------------------------------------------------------- /var1.sh: -------------------------------------------------------------------------------- 1 | echo "What is your name?" 2 | read USER_NAME 3 | echo "Hello $USER_NAME" 4 | echo "I will create you a file called ${USER_NAME}_file" 5 | touch "${USER_NAME}_file" -------------------------------------------------------------------------------- /array_add.sh: -------------------------------------------------------------------------------- 1 | #sum of array shell script 2 | 3 | arr=(10 20 30 40 50) 4 | 5 | sum=0 6 | 7 | for i in ${arr[@]} 8 | do 9 | sum=`expr $sum + $i` 10 | done 11 | 12 | echo $sum -------------------------------------------------------------------------------- /while.sh: -------------------------------------------------------------------------------- 1 | INPUT_STRING=hello 2 | while [ "$INPUT_STRING" != "bye" ] 3 | do 4 | echo "Please type something in (bye to quit)" 5 | read INPUT_STRING 6 | echo "You typed: $INPUT_STRING" 7 | done -------------------------------------------------------------------------------- /SI.sh: -------------------------------------------------------------------------------- 1 | echo "Enter Amount:" 2 | read p 3 | echo "Enter Time:" 4 | read t 5 | echo "Enter ROI:" 6 | read r 7 | 8 | i=` expr $p \* $t \* $r ` 9 | i=` expr $i / 100 ` 10 | 11 | echo "Simple Interest is: $i" -------------------------------------------------------------------------------- /compare_strings.sh: -------------------------------------------------------------------------------- 1 | #shell script to compare two strings 2 | 3 | read -p "Enter two strings: " str1 str2 4 | 5 | if [ $str1 == $str2 ] 6 | then 7 | echo "Equal" 8 | else 9 | echo "Un Equal" 10 | fi -------------------------------------------------------------------------------- /leap_yr.sh: -------------------------------------------------------------------------------- 1 | echo "Enter Year:" 2 | read y 3 | 4 | year=$y 5 | 6 | y=$(( $y % 4 )) 7 | if [ $y -eq 0 ] 8 | then 9 | echo "$year is Leap Year!" 10 | else 11 | echo "$year is not a Leap Year!" 12 | fi -------------------------------------------------------------------------------- /fibo.sh: -------------------------------------------------------------------------------- 1 | read N 2 | a=0 3 | b=1 4 | 5 | echo "The Fibonacci series till $N is : " 6 | 7 | for (( i=0; i 10 | -------------------------------------------------------------------------------- /func3.sh: -------------------------------------------------------------------------------- 1 | myfunc() 2 | { 3 | echo "\$1 is $1" 4 | echo "\$2 is $2" 5 | # cannot change $1 - we'd have to say: 6 | # 1="Goodbye Cruel" 7 | # which is not a valid syntax. However, we can 8 | # change $a: 9 | a="Goodbye Cruel" 10 | } 11 | 12 | ### Main script starts here 13 | 14 | a=Hello 15 | b=World 16 | myfunc $a $b 17 | echo "a is $a" 18 | echo "b is $b" -------------------------------------------------------------------------------- /armstrong.sh: -------------------------------------------------------------------------------- 1 | echo "Enter the number" 2 | read n 3 | function ams 4 | { 5 | t=$n 6 | s=0 7 | b=0 8 | c=10 9 | while [ $n -gt $b ] 10 | do 11 | r=$((n % c)) 12 | i=$((r * r * r)) 13 | s=$((s + i)) 14 | n=$((n / c)) 15 | done 16 | echo $s 17 | if [ $s == $t ] 18 | then 19 | echo "Amstrong number" 20 | else 21 | echo "Not an Armstrong number" 22 | fi 23 | } 24 | result=`ams $n` 25 | echo "$result" -------------------------------------------------------------------------------- /while3.sh: -------------------------------------------------------------------------------- 1 | while read input_text 2 | do 3 | case $input_text in 4 | hello) echo English ;; 5 | howdy) echo American ;; 6 | gday) echo Australian ;; 7 | bonjour) echo French ;; 8 | "guten tag") echo German ;; 9 | *) echo Unknown Language: $input_text 10 | ;; 11 | esac 12 | done < myfile.txt -------------------------------------------------------------------------------- /palin.sh: -------------------------------------------------------------------------------- 1 | read num 2 | 3 | s=0 4 | 5 | rev="" 6 | 7 | temp=$num 8 | 9 | while [ $num -gt 0 ] 10 | do 11 | # Get Remainder 12 | s=$(( $num % 10 )) 13 | 14 | # Get next digit 15 | num=$(( $num / 10 )) 16 | 17 | # Store previous number and 18 | # current digit in reverse 19 | rev=$( echo ${rev}${s} ) 20 | done 21 | 22 | if [ $temp -eq $rev ]; 23 | then 24 | echo "Number is palindrome" 25 | else 26 | echo "Number is NOT palindrome" 27 | fi 28 | -------------------------------------------------------------------------------- /max.sh: -------------------------------------------------------------------------------- 1 | #shell script for largest of n numbers 2 | 3 | echo "Enter Size(N)" 4 | read N 5 | 6 | i=1 7 | max=0 8 | 9 | echo "Enter Numbers" 10 | while [ $i -le $N ] 11 | do 12 | read num 13 | if [ $i -eq 1 ] #set first number as max 14 | then 15 | max=$num 16 | else #from number 2 update max if the num > max. 17 | if [ $num -gt $max ] 18 | then 19 | max=$num 20 | fi 21 | fi 22 | i=$((i + 1)) #increment i by 1 23 | done 24 | 25 | echo $max -------------------------------------------------------------------------------- /calculator.sh: -------------------------------------------------------------------------------- 1 | # function to add two numbers using shell 2 | function add() 3 | { 4 | sum=$(($1 + $2)) 5 | echo "Sum = $sum" 6 | } 7 | function sub() 8 | { 9 | dif=$(($1 - $2)) 10 | echo "Difference = $dif" 11 | } 12 | function multi() 13 | { 14 | mul=$(($1 * $2)) 15 | echo "Product = $mul" 16 | } 17 | function div() 18 | { 19 | quo=$(($1 / $2)) 20 | echo "Quotient = $quo" 21 | } 22 | echo "Enter the first number" 23 | read a 24 | echo "Enter the second number" 25 | read b 26 | 27 | add $a $b 28 | sub $a $b 29 | multi $a $b 30 | div $a $b -------------------------------------------------------------------------------- /func1.sh: -------------------------------------------------------------------------------- 1 | # A simple script with a function... 2 | 3 | add_a_user() 4 | { 5 | USER=$1 6 | PASSWORD=$2 7 | shift; shift; 8 | # Having shifted twice, the rest is now comments ... 9 | COMMENTS=$@ 10 | echo "Adding user $USER ..." 11 | echo useradd -c "$COMMENTS" $USER 12 | echo passwd $USER $PASSWORD 13 | echo "Added user $USER ($COMMENTS) with pass $PASSWORD" 14 | } 15 | 16 | ### 17 | # Main body of script starts here 18 | ### 19 | echo "Start of script..." 20 | add_a_user bob letmein Bob Holness the presenter 21 | add_a_user fred badpassword Fred Durst the singer 22 | add_a_user bilko worsepassword Sgt. Bilko the role model 23 | echo "End of script..." -------------------------------------------------------------------------------- /salary.sh: -------------------------------------------------------------------------------- 1 | echo -e "Enter the value of Basic Salary: \c" 2 | read basic_sal 3 | if [ $basic_sal -ge 0 ] 4 | then 5 | basic=$(expr 1.0*"$basic_sal" | bc) 6 | dp=$(expr 0.5*"$basic"| bc) 7 | temp=$(expr "$basic"+"$dp" | bc) 8 | da=$(expr 0.35*"$temp" | bc) 9 | hra=$(expr 0.08*"$temp" | bc) 10 | ma=$(expr 0.03*"$temp" | bc) 11 | pf=$(expr 0.1*"$temp" | bc) 12 | salary=$(expr "$basic"+"$dp"+"$da"+"$hra"+"$ma"-" $pf" | bc) 13 | 14 | echo "Your Basic Salary: $basic " 15 | echo "Your DP: $dp " 16 | echo "Your DA: $da" 17 | echo "Your HRA: $hra" 18 | echo "Your MA: $ma" 19 | echo "Your PF: $pf" 20 | echo " ------------" 21 | echo "Your Net Salary is Rs. $salary " 22 | else 23 | echo "Please enter a valid Basic Salary " 24 | fi -------------------------------------------------------------------------------- /func5.sh: -------------------------------------------------------------------------------- 1 | adduser() 2 | { 3 | USER=$1 4 | PASSWORD=$2 5 | shift ; shift 6 | COMMENTS=$@ 7 | useradd -c "${COMMENTS}" $USER 8 | if [ "$?" -ne "0" ]; then 9 | echo "Useradd failed" 10 | return 1 11 | fi 12 | passwd $USER $PASSWORD 13 | if [ "$?" -ne "0" ]; then 14 | echo "Setting password failed" 15 | return 2 16 | fi 17 | echo "Added user $USER ($COMMENTS) with pass $PASSWORD" 18 | } 19 | 20 | ## Main script starts here 21 | 22 | adduser bob letmein Bob Holness from Blockbusters 23 | ADDUSER_RETURN_CODE=$? 24 | if [ "$ADDUSER_RETURN_CODE" -eq "1" ]; then 25 | echo "Something went wrong with useradd" 26 | elif [ "$ADDUSER_RETURN_CODE" -eq "2" ]; then 27 | echo "Something went wrong with passwd" 28 | else 29 | echo "Bob Holness added to the system." 30 | fi --------------------------------------------------------------------------------