2016-07-25 33 views
-2

我想逃避以下腳本通過shell命令行選項(/ bin/sh -c)運行它。如何轉義shell腳本以使用command_string選項運行它?

privateIP=$(ifconfig eth0 | grep "inet " | awk \'{print $2}\'); 
sed -i "s/http:\/\/:/http:\/\/$privateIP:/g" init.conf 

請詳細說明答案。

+0

你需要證明你是如何使用'/ bin/sh的-c'。一旦你分配了它,你打算如何處理'$ privateIP'?您確定所返回的價值是否符合您的需求?而WilliamPursell向你展示了當你在str或替換中有'/'字符時替換的正確語法,即''@ http .... @ ..... @ g''。 。如果你得到一個sed錯誤,那麼你應該顯示正在執行的確切命令。祝你好運。 – shellter

回答

2

你的問題是不明確的,但也許你正在尋找:

sh -c 'privateIP=$(ifconfig eth0 | awk "/inet/{print \$2}"); 
    sed -i "[email protected]://:@http://$privateIP:@g" init.conf' 
+0

感謝您的回覆。我試過你的命令,我得到了 - sed:-e表達式#1,字符27:未終止的命令錯誤。僅供參考我試圖運行一個shell腳本.sh文件內嵌/ bin/sh -c選項並堅持轉義腳本 – KarthikJ