Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Bash脚本退出到终端_Bash - Fatal编程技术网

将Bash脚本退出到终端

将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

尝试使此脚本显示数据,导出到文件,然后退出到终端。脚本运行正常,但不会退出。我每次都要按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 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