案例:
[root@localhost rootCodeFiles]# cat if_else_test.sh
#!/bin/sh
echo "请输入一个文件名"
stty erase ^H
read file_name
if [ -d $file_name ]
then
echo "$file_name 是文件夹"
elif [ -f $file_name ]
then
echo "$file_name 是普通文件"
elif [ -c $file_name -o -b $file_name ]
then
echo "$file_name 是设备文件"
else
echo "$file_name 是一种未知文件"
fi
[root@localhost rootCodeFiles]#
结果:
[root@localhost rootCodeFiles]# sh -x if_else_test.sh
+ echo $'\350\257\267\350\276\223\345\205\245\344\270\200\344\270\252\346\226\207\344\273\266\345\220\215'
请输入一个文件名
+ stty erase '^H'
+ read file_name
weizhivar.sh
+ '[' -d weizhivar.sh ']'
+ '[' -f weizhivar.sh ']'
+ '[' -c weizhivar.sh -o -b weizhivar.sh ']'
+ echo 'weizhivar.sh 是一种未知文件'
weizhivar.sh 是一种未知文件
[root@localhost rootCodeFiles]#
附:
read命令下不能退格问题的解决:
解决办法:
在read之前,添加如下命令,重新定义erase:
stty erase ^H
注意:
如果在使用read时,使用-n选项 ,则上述解决办法无效。