read -p "please input a filename:" filename
if test -b "$filename" -o -c "$filename";then
echo "$filename is a device file" && cp $filename /dev
else
echo "$filename is not a device file"
fi
2:模拟 Linux 登陆 Shell
#!/bin/bash
x=wj #这是用户名
y=1111 #这是密码
read -p "please input yourname:" yourname
read -p "please input yourkey:" yourkey
if test "$x" = "$yourname" -a "$y" = "$yourkey" ;then bash #注意test后面比较大小的空格不能省
else
echo"please try again"
fi
3:从键盘读取两个数,并比较两个数大小,并打印结果
#!/bin/bash
read -p "please input num1:" num1
read -p "please input num2:" num2
if test $num1 = $num2 ;then
echo "num1=num2"
elif test $num1 -gt $num2 ;then
echo "num1>num2"
else test $num1 -lt $num2
echo "num1<num2"
fi
4:查找/root/目录下是否存在指定文件
#!/bin/bash
cd /root
read -p "please input filename:" filename
if test -e $filename;then
echo "$filename is exist"
else
echo "$filename is not exist"
fi
5:删除当前目录下大小为 0 的文件
#/bin/bash
for filename in `ls`
do
if test -d $filename;then
b=1
else
a=$(ls -l $filename | awk '{ print $5 }')
if test $a -eq 0;then
rm $filename
fi
fi
done
6:查找最大文件
#!/bin/bash
a=0
for name in *
do
if test -d $name;then
c=1
else
b=$(ls -l $name | awk '{print $5}')
if test $b -ge $a;then
a=$b
namemax=$name
fi
fi
done
echo "the max file is $namemax"
7:打印当前用户(不用 whoami)
#!/bin/bash
echo "Current User is :"echo $(who | sed -n '1p' | awk '{print $1}')
#!/bin/bash
result=1
# $#表示参数个数
while test $# -gt 0
do
result=`expr $1 \* $result`
shift
done
echo "the result is $result"
10:测试一个文件是否已经被排序过
#!/bin/bash
read -p "please input your filename:" file
sort -C $file
# $?表示执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)
a=$?
if test $a -eq 0 ;then
echo "$file is youxu"
else
echo "$file is wuxu"fi
11:判断用户是否在运行
#!/bin/bash
a=$(ps | grep "$$" | awk '{print $2}')
b=$( who| grep "$a" | awk '{print $1}')
read -p "please input a username:" username
if [ "$b" = "$username" ];then
echo "the ueser is running"
else
echo "the ueser is not running"
fi
12:在当前目录下交互式创建文件夹(文件夹已存在则打印提示输入新名字)
#!/bin/bash
read -p "please input dirname:" file
if test -d $file;then
echo dir is exist please try again
else
read -p "create dir now?[y/n] " sel
if test "$sel" = "y";then
mkdir $file
elif test "$sel" = "n";then
bash
else
echo "Your input is wrong"
fi
fi
#!/bin/bash
read -p "please input n:" n
sum=1
for a in `seq 1 $n`
do
sum=`expr $sum \* $a`
done
echo "n! = $sum"
16:利用 case 语句测试输入字符是否是:小写/大写/数字/其他
#!/bin/bash
read -p "please input something:" Key
case $Key in
[[:lower:]]) echo "It is lowercase";;
[[:upper:]]) echo "It is uppercase";;
[0-9]) echo "It is number";;
esac
17:测试文件是否包含特定的文本内容
#!/bin/bash
read -p "enter a string: " string
read -p "enter a filename: " filename
grep -q "$string" $filename #在file中查找str(不输出找到的内容),成功返回0
if test $? -eq 0;then
echo "The text exists in the file"
else
echo "Text does not exist in the file"
fi
18:检测给定的单词是否为词典中的单词
#!/bin/bash
read -p "enter a string:" string
#/usr/share/dict/american-english是ubuntu 系统中英式英语常用单词列表的字典文件
grep -q "$string" /usr/share/dict/american-english
if test $? -eq 0;then
echo "match directionary"
else
echo "not match directionary"
fi