Friday 11 October 2013

Bash Exercise - 2

Script 6: Break 2
LIMIT=19
a=0
while [ "$a" -le "$LIMIT" ]
do
a=$(($a+1))
if [ "$a" -gt 2 ]
then
break # Skip entire rest of loop.
fi
echo -n "$a "
done
echo; echo; echo
exit 0
Script 7: Case
cat << EOF
name me an animal and i will tell ou
ho many legs it has
EOF
read creature
case $creature in
dog|cat) echo " it has four legs";;
hen) echo " it has two legs";;
*) echo wronge choice;;
esac

Script 8: Case 2
#!/bin/bash
echo -e "1) a \n 2) b \n 3) c \n 4) d \n 5) e"
echo "select any one:"
read var1
case $var1 in
a) echo options are
echo -e "1) a1 \n 2)a2 \n 3)a3 enter"
read var2
case $var2 in
a1) echo "one";;
a2) echo "two";;
a3) echo "three";;
*) echo default ;;
esac
;;
b) echo "case two selected";;
c) echo "case three selected";;
*) echo default ;;
esac

Script 9: Case 3
#!/bin/bash
echo "enter yes or no"
read select
case $select in
y|Y|YES|yes) echo u selected yes;;
n|N|no|NO) echo u selected no;;
*) echo select yes or no;;
esac

Script 10: Esac
#!/bin/bash
# escaped.sh: escaped characters

echo; echo

echo "\v\v\v\v" # Prints \v\v\v\v literally.
# Use the -e option with 'echo' to print escaped characters.
echo "============="
echo "VERTICAL TABS"
echo -e "\v\v\v\v" # Prints 4 vertical tabs.
echo "=============="

echo "QUOTATION MARK"
echo -e "\042" # Prints " (quote, octal ASCII character 42).
echo "=============="

# The $'\X' construct makes the -e option unnecessary.
echo; echo "NEWLINE AND BEEP"
echo $'\n' # Newline.
echo $'\a' # Alert (beep).

echo "==============="
echo "QUOTATION MARKS"
# Version 2 and later of Bash permits using the $'\nnn' construct.
# Note that in this case, '\nnn' is an octal value.
echo $'\t \042 \t' # Quote (") framed by tabs.

# It also works with hexadecimal values, in an $'\xhhh' construct.
echo $'\t \x22 \t' # Quote (") framed by tabs.
# Thank you, Greg Keraunen, for pointing this out.
# Earlier Bash versions allowed '\x022'.
echo "==============="
echo


# Assigning ASCII characters to a variable.

No comments :

Post a Comment