2012-08-25 39 views
7

我有用於備份我的系統的這個shell腳本。有一行:帶進程替換的shell腳本中的語法錯誤

tar -Pzcpf /backups/backup.tar.gz --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/log/2> >(grep -v 'socket ignored' >&2) 

正如你所看到的,我一直在努力過濾掉惱人的,無用的「插座忽略」焦油錯誤,使用this blog post

我從在執行殼得到的是:

/斌/ sysback:第45行:附近意外的標記>' /bin/sysback: line 45:焦油-Pzcpf /備份/備份--directory =/ --exclude =語法錯誤PROC --exclude = SYS --exclude =開發/ PTS --exclude =備份--exclude =無功/日誌/ 2>>(grep的-v '插座忽略'> & 2)」

+0

我想'2>>('應該是'2>('。 –

+0

沒有給該變更後的語法錯誤,但並沒有任何過濾的輸出。 –

+2

>(...)進程替換語法是一個非標準功能,並且你的shell明顯不支持它。使用不同的shell,或者可能是更新版本的bash。 –

回答

17

您使用的語法是基本shell語法的bash擴展,因此您必須小心使用bash運行腳本。 (Ksh也有>(…)進程替換,但在重定向後不支持它,Zsh會沒事的)

鑑於您收到的錯誤消息,您在bash中運行此腳本,但在其POSIX兼容模式下,而不是完整的bash模式。注意用明確的#!/bin/bash行來調用腳本。 #!/bin/sh不會這樣做,即使/bin/sh是bash的符號鏈接,因爲如果以名稱sh調用bash,bash將以POSIX模式運行。如果您使用bash功能,請始終按名稱調用bash。

如果您想使用bash功能,請注意不要設置環境變量POSIXLY_CORRECT或在命令行上傳遞--posix選項。

或者,不要使用這種特定於bash的語法;使用便攜式結構,如Stephane Rouberol提出的結構。

7

如何:

tar -Pzcpf /backups/backup.tar.gz --directory=/ \ 
    --exclude=proc --exclude=sys --exclude=dev/pts \ 
    --exclude=backups --exclude=var/log 2>&1 | grep -v 'socket ignored' 
+1

即使你在這裏提供的是解決方案的合法替代品,我會選擇@Gilles解決方案作爲正確的答案,因爲它回答了他提出的問題。不過謝謝你的回答。 –

0

我發現,在Gentoo此外,如果SH是爲/ bin/bash的一個鏈接,如果你打電話與「sh‘的腳本名’」腳本不運行它作爲一個bash腳本和失敗:

matchmorethan.sh: line 34: syntax error near unexpected token `<' 
matchmorethan.sh: line 34: `done < <(cat $matchfile)' 

所以如果你需要使用Process Substitution功能,你需要專門用bash運行它。但我沒有找到任何這方面的參考。

1

實際上,當GNU tar提供忽略「套接字忽略」警告的選項時,您不必在std錯誤中進行這樣的重定向。

tar --warning='no-file-ignored' -Pzcpf /backups/backup.tar.gz --directory=/ --exclude=proc --exclude=sys --exclude=dev/pts --exclude=backups --exclude=var/log/2> new.err 

You could find the original link with more ignore options here

+0

感謝您的評論,但特別感謝鏈接:) –