[英]Shell script to nohup every file in a directory
這是對此處發布的問題的跟蹤,該Shell腳本針對輸入的文件名執行nohup 。
我只是想找出修改下面的no_hup腳本以針對整個SQL腳本目錄而不是單個文件執行的方法。 因此,我試圖找到一種很好的方法來修改以下腳本以對整個文件目錄執行:
如何編寫外殼程序腳本,以便可以在名為test的目錄中運行,該目錄包含文件foo1.sql,foo2.sql,foo3.sql
./nohup_sh測試
這將產生輸出
nohup psql -d db -f test / foo1.sql>&test / foo1.out&
nohup psql -d db -f test / foo2.sql>&test / foo2.out&
nohup psql -d db -f test / foo3.sql>&test / foo3.out&
這是我從上一個答案(稱為nohup_sh)中一直使用的代碼
#!/bin/bash
outputFile="$(echo $1 | cut -d\. -f 1).out"
nohup psql -d db -f "$1" >& "$outputFile" &
#!/bin/bash
for file; do
nohup psql -d db -f "$file" >& "${file%.*}.out" &
done
${file%.*}
是bash參數擴展 ,執行與cut
命令相同,但bash builtin
for file
是for file in "$@"
for file
的簡寫
用法:
./script.bash sql_dir/*.sql
要么
./script.bash *.sql
以下腳本在作為參數傳遞的目錄中搜索任何*.sql
文件,並執行所需的nohup命令。 找到的文件名用空字節分隔,以避免文件名中的空格或通配符出現問題。
#!/bin/bash
if [[ -d "$1" ]]; then
find "$1/" -type f -name "*.sql" -print0 | while read -rd $'\0' file; do
nohup psql -d db -f "$file" >& "${file%.*}.out" &
done
else
echo "$1 is no directory"
exit 1
fi
exit 0
像script.sh somedir
這樣稱呼它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.