Monday 28 October 2013

Bash Exercise - 8

Script 36: Nested For
#!/bin/bash
# nested-loop.sh: Nested "for" loops.

outer=1 # Set outer loop counter.

# Beginning of outer loop.
for a in 1 2 3 4 5
do
echo "Pass $outer in outer loop."
echo "---------------------"
inner=1 # Reset inner loop counter.

# ===============================================
# Beginning of inner loop.
for b in 1 2 3 4 5
do
echo "Pass $inner in inner loop."
let "inner+=1" # Increment inner loop counter.
done
# End of inner loop.
# ===============================================

let "outer+=1" # Increment outer loop counter.
echo # Space between output blocks in pass of outer loop.
done
# End of outer loop.

exit 0

Script 37: Create New File
#!/bin/bash
if test -e new
then
echo "file exist"
else
touch new
fi


Script 38: For
#!/bin/bash
sum=0
for var in $1 $2 $3 $4 $5
do
sum=$(($sum + $var))
done
echo "sum is $sum"

Script 39: Check whether the two numbers are equal or not
#!/bin/bash

echo "two values"

read num1 num2

if [ $num1 -ne $num2 ]

then

echo "two not are equal"

fi

Script 40: Open File
#!/bin/bash
echo "enter the file name to be opened"
read file_name
#cat $file_name
echo "number of characters in the file is:"
wc -m $file_name
echo "number of lines in the file is:"
wc -l $file_name
echo "number of words in the file is:"
wc -w $file_name

No comments :

Post a Comment