我有一個 bash 腳本設定,其中我eval呼叫了一個變數snake,如果出現錯誤則退出。即使執行snake命令沒有錯誤,aws s3下面的命令也不會執行。
如果我洗掉|| echo "ERROR OCCURED, LOOK ABOVE. EXITING" ; exit 1了,則將執行 aws 命令。
我知道沒有錯誤,eval $snake因為echo "ERROR OCCURED, LOOK ABOVE. EXITING"沒有回傳到標準輸出(僅在實際出現錯誤時)。
我需要在成功運行后執行 aws 命令eval $snake,但我不確定如何執行此操作。
s3='ebio/'
# do not edit contents below unless needed
snake="snakemake --default-remote-provider S3 --default-remote-prefix '$s3' --use-conda --cores 32 --rerun-incomplete --printshellcmds"
read -p "Rewrite over samples.tsv, peak_norm.tsv, and treated_vs_untreated.tsv files? (y/n)" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
# exit if snake fails
eval $snake || echo "ERROR OCCURED, LOOK ABOVE. EXITING" ; exit 1
# remove temp files
aws s3 rm --recursive s3://"$s3"rep_element_pipeline --exclude "*" --include "*tmp"
aws s3 rm --recursive s3://"$s3"rep_element_pipeline --exclude "*" --include "*sam.parsed"
aws s3 rm --recursive s3://"$s3"rep_element_pipeline --exclude "*" --include "*sam.parsed.done"
aws s3 rm --recursive s3://"$s3"rep_element_pipeline --exclude "*" --include "*.sam.tmp.combined_w_uniquemap.rmDup.sam"
aws s3 rm --recursive s3://"$s3"rep_element_pipeline --exclude "*" --include "*.sam.tmp.combined_w_uniquemap.prermDup.sam"
aws s3 rm --recursive s3://"$s3"rep_element_pipeline --exclude "*" --include "*.adapterTrim.round2.rmRep.bam.tmp"
aws s3 rm --recursive s3://"$s3"rep_element_pipeline --exclude "*" --include "*.fastq.gz.mapped_vs_bt2_hg38.sam"
aws s3 rm --recursive s3://"$s3"data_analysis --exclude "*" --include "*.saturations_reads.txt"
fi
uj5u.com熱心網友回復:
這里的直接問題是,無論退出狀態是什么foo || bar; baz,都會發生 in 。在這方面,分號作為命令分隔符的處理方式與換行符相同。這可以通過顯式分組來解決,例如- 或者更明確地,bazfoofoo || { bar; baz; }if foo; then bar; baz; fi
相關代碼最好寫成:
# multi-line form optional, but lets you add comments after each line
snake=(
snakemake
--default-remote-provider S3
--default-remote-prefix '$s3' # FIXME: Sure you don't want "$s3" instead?
--use-conda
--cores 32
--rerun-incomplete
--printshellcmds
)
"${snake[@]}" || { echo "ERROR OCCURRED, LOOK ABOVE, EXITING" >&2; exit 1; }
將命令存盤在陣列或函式中而不是字串中可以避免eval和它帶來的安全問題;這兩種技術都在BashFAQ #50中講授,問題eval在BashFAQ #48中有詳細介紹。
使用花括號將它們放在exit 1同一組中,以echo確保它們一起發生或根本不發生。
請注意,'$s3'僅當您希望將確切的字串$s3作為默認遠程前綴傳遞時才是正確的;如果您希望使用名為 s3 的變數的內容,請將其更改為"$s3".
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/422221.html
標籤:
上一篇:為什么通過xargs管道傳輸時subshel??l命令不能與echo一起使用?
下一篇:Bash中的變數
