#!/bin/sh
clear
echo -n "Enter command for directory info: "
read
我不知道从那里去哪里。我很困惑变量如何工作以获得所需的输出。
我需要更加具体。当用户输入类似" dirinfo -f"的内容时,我需要此输出。一旦提示用"输入命令进行目录信息"回声如上。我不知道该怎么做。
答案 0 :(得分:1)
请注意,StackOverflow会以非常非常高的频率被Google编入索引 - 将其作为家庭作业转而你 会被抓住。
希望研究它所使用的技术会对你有所帮助。
如果此脚本保存为dirinfo
,设置为可执行,并置于用户的路径中,则在shell提示符下运行dirinfo -f
或dirinfo -d
将表现为根据需要。
#!/bin/sh
# check whether first argument is -f
if [ "$1" = "-f" ]; then
# check whether each result is a file
for f in *; do
[ -f "$f" ] && count=$(( count + 1 ))
done
echo "$count"
elif [ "$1" = "-d" ]; then
# use a glob that returns only directories for better efficiency
# overwrites the argument list ("$@") with a list of directory names
set -- */
if [ -d "$1" ]; then
# number of arguments is number of directories
echo "$#"
else
# if nothing matched, we got back "*/", for which [ -d '*/' ] returns false
# (unless there's literally a directory with a name of '*').
echo 0
fi
else
# this was used incorrectly
echo "Usage: $0 [-d | -f]"
done
答案 1 :(得分:0)
您可以使用 ls 和 wc
echo $(ls | wc -l)