2007/12/25

Shell Script Basic bash

//b1
#!/bin/bash

family=tiangtae
name=narathip
echo "I am ${name} ${family}"


//b2
#!/bin/bash

age1=18
age2=25

echo "I am ${age1} years old"
echo "He is ${age2} years old"

//b3
#!/bin/bash

age1=18
age2=`expr $age1 + 7`

echo "I am ${age1} years old"
echo "He is ${age2} years old"


//b4
#!/bin/bash

echo $@
echo $#
echo $0
echo $1


//b5
#!/bin/bash

hour=`date +%H`

if [ "$hour" -lt 12 ]
then
echo "AM:"
else
echo "PM:"
fi


//b6
#!/bin/bash

hour=`date +%H`

if [ "$hour" -lt 12 ]
then
echo "AM:"
else
echo "PM:"
fi

# =
# !=
# -eq
# -ne
# -gt
# -ge
# -lt
# -le


//b7
#!/bin/bash

echo "Put the number: "
read num

if [ "$num" -lt 100 ]
then
echo "$num is less than 100"
elif [ "$num" -lt 1000 ]
then
echo "$num is less than 1000 and more than 100"
else
echo "$num is more than 1000"
fi


//b8
#!/bin/bash

echo "Put your score: "
read num

if [ "$num" -ge 0 -a "$num" -lt 7 ]
then
echo "You have to Phayayamm much more!"
elif [ "$num" -ge 7 -a "$num" -lt 15 ]
then
echo "Thammada!"
elif [ "$num" -ge 15 -a "$num" -le 20 ]
then
echo "Great!"
else
echo "Your score must be 0-20."
fi


//b9
#!/bin/bash

echo "Write your teacher: "
read name

if [ "$name" = "Narathip" -o "$name" = "Thanyarak" ]
then
echo "You are class A"
elif [ "$name" = "Somchai" -o "$name" = "Somsri" -o "$name" = "Somyot" ]
then
echo "You are class B"
elif [ "$name" = "Tukky" -o "$name" = "Tsubasa" ]
then
echo "Your are Johnny Junior's fan"
else
echo "Unavailable!"
fi


//b10
#!/bin/bash

if [ "$1" = "" ]
then
echo "Write your teacher'name in command line."
elif [ "$1" = "Narathip" -o "$1" = "Thanyarak" ]
then
echo "You are class A"
elif [ "$1" = "Somchai" -o "$1" = "Somsri" -o "$1" = "Somyot" ]
then
echo "You are class B"
elif [ "$1" = "Tukky" -o "$1" = "Tsubasa" ]
then
echo "Your are Johnny Junior's fan"
else
echo "Unavailable!"
fi


//b11
#!/bin/bash

while [ "$1" != "" ]
do
echo $1
shift
done


//b12
#!/bin/bash

num=3
while [ "$num" -lt 10 ]
do
echo '$num'" is $num"
num=`expr $num + 1`
done


//b13
#!/bin/bash

num=$1
while [ "$num" -lt 10 ]
do
echo '$num'" is $num"
num=`expr $num + 1`
done


//b14
#!/bin/bash

num=$RANDOM

echo "Put your favorite number: "

while true
do
echo "Put --> 0-32767"
read x
if [ $x -gt $num ]
then
echo "less than your number!"
elif [ $x -lt $num ]
then
echo "more than your number"
else
echo "OK!"
break
fi
done


//b15
#!/bin/bash

i=1
while [ "$i" -le 100 ]
do
touch temp${i}.txt
i=`expr $i + 1`
done


//b16
#!/bin/bash

i=1
while [ "$i" -le 100 ]
do
if [ "$i" -lt 10 ]
then
num=00${i}
elif [ "$i" -lt 100 ]
then
num=0${i}
else
num=${i}
fi

mv temp${i}.txt temp${num}.txt
i=`expr $i + 1`
done


//b17
#!/bin/bash

while [ "$1" != "" ]
do
name=`echo $1 cut -f1 -d'.'`
echo $1, $name
shift
done


//b18
#!/bin/bash

while [ "$1" != "" ]
do
name=`echo $1 cut -f1 -d'.'`
fname=${name}_e.html
echo $1, $fname
shift
done


//b19
#!/bin/bash

while [ "$1" != "" ]
do
name=`echo $1 cut -f1 -d'.'`
fname=${name}_e.html
cat $1 > $fname
mv $fname ~/public_html
chmod 604 ~/public_html/$fname
shift
done


//b20
#!/bin/bash

i=1
while [ "$i" -le 100 ]
do
echo "***** ${i} *****"
if [ -e temp0${i}.txt ]
then
echo "temp0${i}.txt already exists !"
fi
i=`expr $i + 1`
done


//b21
#!/bin/bash

i=1
while [ "$i" -le 100 ]
do
echo "***** ${i} *****"
if [ ! -e temp${i}.txt ]
then
touch tmp${i}.txt
fi
i=`expr $i + 5`
done


//b22
#!/bin/bash

for file in tmp1.txt tmp4.txt tmp6.txt
do
echo "*** ${file} ***"
done


//b23
#!/bin/bash

for file in `ls tmp*.txt`
do
echo "*** ${file} ***"
done


//b24
#!/bin/bash

for file in $(ls tmp*.txt)
do
echo "*** ${file} ***"
done


//b25
#!/bin/bash

for file in `ls tmp*.txt`
do
i=`echo $file sed -e "s/tmp//" sed -e "s/.txt//"`

if [ "$i" -lt 10 ]
then
num=00${i}
elif [ "$i" -lt 100 ]
then
num=0${i}
else
num=${i}
fi

mv tmp${i}.txt tmp${num}.txt
done


//b26
#!/bin/bash

for file in `ls tmp*.txt`
do
i=`echo $file sed -e "s/[a-z.]//g"`

if [ "$i" -lt 10 ]
then
num=00${i}
elif [ "$i" -lt 100 ]
then
num=0${i}
else
num=${i}
fi

mv tmp${i}.txt tmp${num}.txt
done

No comments: