Shell Script Examples
Shell Script Examples
#!/bin/bash
do
done
printf "\n"
#!/bin/bash
n=10
if [ $n -lt 10 ];
then
else
fi
#!/bin/bash
do
done
printf "\n"
a=true
count=1
while [ $a ]
do
echo $count
if [ $count -eq 10 ];
then
break
fi
((count++))
done
echo
"input the cost price of an item"
read cp
read sp
then
fi
then
s=$((cp - sp))
echo "loss of rs:$s"
else
s=$((sp - cp))
fi
#!/bin/bash
echo "enter value of n"
read rows
read column
do
do
done
echo
done
#!/bin/bash
echo "enter value of n"
read rows
do
do
done
echo
done
read rows
for((i=1;
i<=rows; i++))
do
do
done
echo
done
read num
for (( i=1;i<=$num ;i++))
do
for (( j=$num;j>=i;j-- ))
do
done
for (( c=1;c<=i;c++ ))
do
sum=`expr $sum + 1`
done
echo ""
done
d_max=`expr $num - 1`
for (( i=$d_max;i>=1;i--))
do
for (( j=i;j<=$d_max;j++ ))
do
if [ $j -eq $d_max ]
then
fi
done
for (( c=1;c<=i;c++ ))
do
sum=`expr $sum + 1`
done
echo ""
done
Steps
1. Start
2. read number
4. reminder=number%10
5. sum=sum+(reminder*reminder*reminder)
6. number=number/10
8. if sum = temp
10. else
12. stop
read n
function ams
t=$n
s=0
b=0
c=10
while [ $n -gt $b ]
do
r=$((n % c))
i=$((r * r * r))
s=$((s + i))
n=$((n / c))
done
echo $s
if [ $s == $t ]
then
else
fi
result=`ams $n`
echo "$result"
read rows
#-n -> allows not to append to next line
for((i=1;
i<=rows; i++))
do
do
done
echo
done
read a
read b
read c
if [ $a -gt $b -a $a -gt $c ]
then
then
else
fi
shell script to make a basic calculator which performs
addition, subtraction, Multiplication, division
echo "2.
Subtraction"
read c
echo "give value of a and b"
read a
read b
case $c in
esac
read n
i=2
while [ $i -lt $n ]
do
sum=$((sum+i))
i=$((i+2))
done
echo "sum:"$sum
#!/bin/bash
arr[0]="zero"
arr[1]="one"
arr[2]="two"
arr[3]="three"
arr[4]="four"
arr[5]="five"
echo "array size = ${#arr[*]}"
echo "array items"
for item in ${arr[*]}
do
echo $item
done
echo "array items"
for item in ${arr[@]}
do
echo $item
done
echo "array indexes"
for index in ${!arr[*]}
do
echo $index
done
echo "index: array"
for index in ${!arr[*]}
do
echo $index ${arr[$index]}
done
#!/bin/bash
echo
echo "-- with double-quot"
for item in "${arr2[@]}"
do
echo $item
done
echo
Write shell script to Generate ramdom number array with 10 elements and then sort:
#!/bin/bash
#!/bin/bash
echo "Enter the size of array"
read size
echo "Enter $size elements"
for((i=0;i<size;i++))
do
read arr[$i]
done
echo "Contents of array:"
for((i=0;i<size;i++))
do
echo ${arr[$i]}
done