2013-02-18 99 views
1

我有一個需要來源的bash文件。用戶可能不知道csh(默認配置),所以我想將shell更改爲bash,但是源自該用戶的意圖。from csh to bash and re-source the same file

有很多的解決這個幫助和產生的代碼將是:

#!/bin/csh (AND bash!) 
[ "$?shell" = "1" ] && bash --login -c 'source this_file; bash' && exit 
... 

一切正常,除了一個事實,即源文件this_file必須硬編碼。在csh $_中將包含source this_file,因爲那是開始採購腳本的命令,但是我無法將它傳遞給bash命令。

這意味着:

  1. 如果我使用:

    ... && set this_file=($_) && bash -c "$this_file; bash" && ...

    bash將抱怨括號是錯誤的(這種情況發生的慶典開始了第二次嘗試源this_file

  2. 如果我使用:

    ... && bash -c ""$_"; bash" && ...

    慶典得到一個破碎的命令無法正常工作或:bash -c "source" "this_file" "; bash"

  3. 如果我使用:

    ... && bash --login -c "$_; bash" && ...

    CSH得到一個破碎的命令:'不匹配」。

我不能找出如何使用$_與傳遞值作爲單個命令(即bash -c "source this_file; bash"

這是測試一個公認的bash語法:

cat >a.sh<<'EOF' 
[ "$?shell" = "1" ] && bash --login -c 'source a.sh; bash' && exit 
a=1 
EOF 

然後我期待這個工作:

$ csh -c 'source a.csh' 
$ echo $a 
1 

我很確定它曾經工作K ...我試圖找出它爲什麼不現在。 (我使用tcl模塊包解決了這個問題,但我會試試)

+0

將不會$ 0給你訪問腳本的名稱? – kdubs 2013-02-18 14:21:43

+0

@kdubs否,因爲它是來源。 – estani 2013-02-18 14:23:03

+0

當然,你是對的。它會顯示原始程序。當時無法嘗試。 – kdubs 2013-02-19 14:30:25

回答

1

可以將參數傳遞給bash -c 'cmd'構造,即bash -c 'echo [email protected]' arg0 1 2 3 4 5

#!/bin/csh (AND bash!) 
#[ "$?shell" = "1" ] && bash --login -c 'source this_file; bash' && exit 
[ "$?shell" = "1" ] && bash --login -c '${@}; bash' arg0 $_ && exit 
+0

這聽起來很有希望,我不知道它。我不能在這個時候工作(不是我以前的解決方案: - /)。一旦我驗證它的作品,我會接受答案。 – estani 2013-03-18 18:59:18

相關問題