1

试图让这个脚本显示数据,导出到文件,然后退出到终端。脚本运行良好,但不会退出。我每次都必须按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
4

3 回答 3

0

如果当前脚本是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."
于 2013-04-17T19:56:00.937 回答
0

您可以使用 命令分组 来避免重复tee调用

{
  set $(find . -type d | wc -l)
  echo "Number of subdirectories in this directory: $*"

  set $(ls -1 | wc -l)
  echo "List of files in the current directory: $*"

  set $(find -size 0)
  echo "List of zero-length files in current directory: $*"

  set $(du -sh)
  echo "Used storage space of the current directory is: $*"

  echo "Data output of 'dirchk.sh' is in this directory called"
  echo "directory-check.results."
} | tee directory-check.results
于 2013-04-17T19:57:08.153 回答
-1

编辑:好的,我明白了。错误出现在脚本的末尾,不会让您退出。我可以建议你使用这样的功能吗?

#!/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
于 2014-04-16T10:08:55.193 回答