题目要求
1.检查输入的路径是否为空,如果路径为空,提示输入一个存储在的路径
2.检查输入的路径是个文件还是目录,如果不是文件,也不是目录,把它创建为文件
3.如果路径是个文件,把/bin/所有的文件保存到文本中
思路
这里涉及到 shell 的test语句,test命令的博文,可以先看一下这篇博文,熟悉一下test命令是如何使用的,然后根据题目,先来一个循环,执行体包含提示用户输入,并且判断输入的内容是否为空或者是否存在,不是便再次提醒,还不是,便提醒用户会帮用户创建文件,
while执行语句中的if 判断 用户是否输入内容,且输入的内容是否是“存在”,
执行语句完成之后,便进入第二个if语句(while执行体中的语句有两个if,但功能相同,所以归为一类)
第二个if语句判断输入是否为文件或者目录,先判断是否为目录,yes,则提醒用户您输入的是目录
若不是目录,则提醒用户bash将会ls >> userInput(即第三题中的内容,我将题目做了一点点小的改动)
语句过后证明既不是文件也不是目录 如用户输入 " /home/abcderfdafdf",这样abcderfdafdf既不是文件也不是目录,则提醒用户会根据用户输入的内容帮用户创建一个文件,如用户输入"/home/abcd",
则执行 touch userInput
#!/bin/bash
#this is a reculation,users could input twice
#if the input is existing , break immediately
#but after two times input , things inputed still is not exist , remind user
#then, judge if the input is dic or file
# yes go on ,
# no touch the input
while true
do
echo "please input a path :"
read pathInput
if [ -e $pathInput ] && [ -n "$pathInput" ]
then
break;
else
echo "please input a existed path , bitch :"
read pathInput
if [ -s $pathInput ] && [ -n "$pathInput" ]
then
break;
fi
fi
echo " bitch , i warn U"
echo " i will create a file"
break;
done
if [ -d $pathInput ] || [ -f $pathinput ]
then
if [ -d $pathInput ]
then
echo " what U input is a dic"
exit
fi
if [ -f $pathInput ]
then
echo " I will collect file from /bin to the file "
ls /bin/ >> $pathInput
exit
fi
fi
echo " I will create a file by Your path "
touch $pathInput
在写shell命令的过程中遇到了一下问题,如变量的细节应用,"",’’,``的区别,以及一个小问题,就是
变量的赋值,=两边不能有空格,自己需要在今后中多多注意