根据Linux命令行与shell脚本编程大全 练习测试的shell
#!/bin/bash
#using basename with $0 parameter
#echo 'hello,world'
echo "this costs \$15"
echo HOME : $HOME
var1=test1
num1=11
echo "$var1 $num1"
var1=test2
num2=22
echo "$var1 $num2"
date_=`date`
echo "today is "$date_
#ls /usr/bin -l > `date +%y_%m_%d_%H_%M_%S`.log
var3=20
var4=10
res1=`expr $var3 / $var4`
echo $res1
#res2=$[ $var3 / $var4 ]
#echo $res2
if date
then
echo "it works"
else
echo "no works"
fi
var5=20
var6=21
if [ $var5 -gt $var6 ]
then
echo "$var5 > $var6 "
else
echo "$var5 <= $var6"
fi
if [ $var5 -ne $var6 ]
then
echo "no equal"
fi
var7=test111
var8=test111
if [ $var7 = $var8 ]
then
echo same
else
echo no same
fi
if [ -n "$var7" ]
then
echo "$var7 is not empty"
fi
if [ -d $HOME ]
then
echo "is_dir"
else
echo "no dir"
fi
if [ -O /etc/passwd ]
then
echo "you are owner"
else
echo "you are not owner"
fi
if (( $var6 < 0 ))
then
echo "$var6<0"
else
echo "$var6>=0"
fi
if [[ $USER=r* ]]
then
echo "reg ok"
fi
case $USER in
root)
echo 'root';;
test)
echo 'test';;
*)
echo 'default';;
esac
for test in a b c d e
do
echo $test
done
echo $test
IFS=$'\n'
for test in `cat /etc/passwd`
do
echo $test
done
for file in /tmp/*
do
if [ -d "$file" ]
then
echo "$file is dir"
elif [ -f "$file" ]
then
echo "$file is file "
fi
done
#for(( a = 0 ; a <= 10 ; a++ ));
#do
#echo "test num $a"
#done
#for (( i=0; i<10; i++)); do
#echo $i
#done
var9=10
while [ $var9 -gt 0 ]
do
echo $var9
var9=`expr $var9 - 1 `
done
par=$#
echo "\$0 =`basename $0`; \$1 =$1 ;there is $# parameters;last parameter is $par"
for param in "$*"
do
echo "\$* param #$count=$param"
done
count=1
cat 1.txt | while read line
do
echo "line $count : $line"
count=` expr $count + 1 `
done
echo 'finished'
echo "error">&2
echo "normal"
exec 2>testerror
exec 1>testout
if read -s -t 5 -p "enter a number :" n
then
echo "you enter is $n"
else
echo
echo "slow man"
fi