簡體   English   中英

退出Bash腳本到終端

[英]Exit Bash Script to terminal

嘗試獲取此腳本以顯示數據,導出到文件,然后退出到終端。 腳本運行正常,但不會退出。 我必須每次都按Ctrl + c。 我嘗試過命令殺死,返回和退出均未成功。 感謝任何建議。 這讓我瘋狂。

#!/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調用

{
  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

編輯:好的,我明白了。 錯誤出現在腳本的結尾,該腳本不允許您退出。 我可以建議您使用這種功能嗎?

#!/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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM