比较两个数大小
#/bin/bash
echo "please enter two number"
read a
read b
if [ $a -eq $b ]
then echo "$a = $b"
elif test [ $a -gt $b ]
then echo "$a > $b"
else echo "$a < $b"
fi
查找/root/目录下是否存在该文件
#/bin/bash
echo "enter a file name:"
read a
if [ -e /root/$a ]
then echo "the file is exist!"
else echo "the file is not exist!"
fi
for循环应用
#/bin/bash
clear
for num in 1 2 3 4 5 6 7 8 9 10
do
echo "$num"
done
判断用户是否登录
#/bin/bash
echo "Please enter a user:"
read a
b=$(whoami)
if test $a = $b
then echo "the user is running."
else echo "the user is not running."
fi
删除当前目录下大小为0的文件
#/bin/bash
for filename in `ls`
do
if test -d $filename
then b=0
else
a=$(ls -l $filename | awk '{ print $5 }')
if test $a -eq 0
then rm $filename
fi
fi
done
创建文件夹
#/bin/bash
while :
do
echo "please input file's name:"
read a
if test -e /root/$a
then
echo "the file is existing Please input new file name:"
else
mkdir /root/$a
echo "you aye sussesful!"
break
fi
done
查找最大文件
#/bin/bash
a=0
for name in *.*
do
b=$(ls -l $name | awk '{print $5}')
if test $b -ge $a
then a=$b
namemax=$name
fi
done
echo "the max file is $namemax"
case语句练习
#!/bin/bash
clear
echo "enter a number from 1 to 5:"
read num
case $num in
1) echo "you enter 1"
;;
2) echo "you enter 2"
;;
3) echo "you enter 3"
;;
4) echo "you enter 4"
;;
5) echo "you enter 5"
;;
*) echo "error"
;;
esac
内置命令的使用(可以做开机提示语)
#/bin/bash
clear
echo "Hello, $USER"
echo
echo "Today 's date is `date`"
echo
echo "the user is: `who`"
echo
echo "this is `uname -s`"
echo
echo "that's all folks! "
echo "dir root "
ls -la /root
OR
#/bin/bash
clear
echo "Hello, $USER"
echo
echo "Today 's date is `date`"
echo
echo "the user is: "
who
echo
echo "this is `uname -s`"
echo
echo "that's all folks! "
转载于:https://blog.51cto.com/tfbaby/1317860