将Bash脚本退出到终端
尝试使此脚本显示数据,导出到文件,然后退出到终端。脚本运行正常,但不会退出。我每次都要按Ctrl+c键。我尝试过kill、return和exit命令,但没有成功。谢谢你的建议。这快把我逼疯了将Bash脚本退出到终端,bash,Bash,尝试使此脚本显示数据,导出到文件,然后退出到终端。脚本运行正常,但不会退出。我每次都要按Ctrl+c键。我尝试过kill、return和exit命令,但没有成功。谢谢你的建议。这快把我逼疯了 #!/bin/bash #Script that displays data about current directory. echo echo -n "Number of subdirectories in this directory: " find . -type d | wc -l sleep
#!/bin/bash
#Script that displays data about current directory.
echo
echo -n "Number of subdirectories in this directory: "
find . -type d | wc -l
sleep 2
echo
echo -n "List of files in the current directory: "
ls -1 | wc -l
sleep 2
echo
echo "List of zero-length files in current directory: "
find -size 0
sleep 2
echo
echo "Used storage space of the current directory is: "
du -sh
sleep 2
echo
echo -n "Data output of 'dirchk.sh' is in this directory called directory-check.results."
./dirchk.sh > directory-check.result
如果当前脚本是
dirchk.sh
,那么它将在无限循环中运行dirchk.sh
运行dirchk.sh
,它运行dirchk.sh
。。。要避免这种情况,请使用tee
命令:
#!/bin/bash
#Script that displays data about current directory.
echo
echo -n "Number of subdirectories in this directory: "
(find . -type d | wc -l 2>&1) | tee directory-check.result
sleep 2
echo
echo -n "List of files in the current directory: "
(ls -1 | wc -l 2>&1) | tee -a directory-check.result
sleep 2
echo
echo "List of zero-length files in current directory: "
(find . -size 0 2>&1) | tee -a directory-check.result
sleep 2
echo
echo "Used storage space of the current directory is: "
(du -sh 2>&1) | tee -a directory-check.result
sleep 2
echo
echo -n "Data output of 'dirchk.sh' is in this directory called directory-check.results."
你可以用
避免重复的tee
调用
{
设置$(查找-类型d | wc-l)
echo“此目录中的子目录数:$*”
设置$(ls-1 | wc-l)
echo“当前目录中的文件列表:$*”
设置$(查找-大小为0)
echo“当前目录中长度为零的文件列表:$*”
设置$(du-sh)
echo“当前目录的已用存储空间为:$*”
echo“dirchk.sh”的数据输出位于名为
echo“目录检查。结果。”
}| tee目录检查.results
编辑:好的,我明白了。错误出现在脚本末尾,不允许您退出。
我可以建议您使用像这样的函数吗
#!/bin/bash
#Script that displays data about current directory.
echo
testing () {
echo "Number of subdirectories in this directory: $(find . -type d | wc -l)"
sleep 2
echo
echo "List of files in the current directory: $(ls -1 | wc -l)"
sleep 2
echo
echo "List of zero-length files in current directory: $(find -size 0)"
sleep 2
echo
echo "Used storage space of the current directory is: $(du -sh)"
sleep 2
echo
}
testing 2>&1 |tee directory-check.results && echo "Data output of dirchk.sh is in this directory called directory-check.results."
exit