2017-03-07 43 views
0

我正嘗試創建一個shell腳本,它使用root訪問權限來安裝所有依賴項,並且在完成它之後,它將從root命令退出並繼續執行該腳本與普通用戶一樣。在shell腳本中執行root命令,並在進程後更改爲普通用戶

這是測試代碼:

#!/bin/sh 

output=$(whoami) 

if [ "$(whoami)" != "root" ] 
then 
    su -c "echo \"hi\"" 
    echo $output 
    //continue doing some installtion 
    exit 
fi 

echo $output //this should show the normal username not the root name 

回答

0
#!/bin/sh 

su -c 'echo $(whoami)' 
echo $(whoami) 

當你通過與命令的選項下面-c它會作爲用戶,所以當你想安裝任何依賴關係,您可以運行以下命令,如上例所示。

+0

我試過這種方法有沒有其他方法? – anonymous5671

相關問題