2013-06-27 57 views
2

我想製作一個shell腳本來打開一個文件並在文件末尾添加行,然後保存它。如何製作一個shell腳本來打開一個文件並在文件末尾添加行並保存?

更具體,我想作以下命令shell腳本:

$ ulimit -n 
1024 

如果小於65536那麼,

$ vim /etc/security/limits.conf 

添加在文件的結尾:

root soft nofile 65536 
root hard nofile 65536 
soft nofile 65536 
soft nofile 65536 

!wc in vim。然後重啓。

如何製作這個shell腳本?

回答

2
if [ `ulimit -n` -lt 65536 ]; then 
    { 
    echo "root soft nofile 65536" 
    echo "root hard nofile 65536" 
    echo "soft nofile 65536" 
    echo "soft nofile 65536" 
    } >> /etc/security/limits.conf 
    reboot 
fi 
+3

或者,更好:'{回聲「......」;回聲「...」;回聲「...」;回聲「...」; } >>/etc/security/limits.conf'只打開(關閉)文件一次。 –

+0

@JonathanLeffler這太棒了,謝謝你的提示。 – Amit

+1

在評論中,我需要分號;在你的腳本中,最好是可讀性用新行代替它們,如圖所示。如果'}與命令位於同一行,那麼在它之前你需要一個分號,不同於子shell中的')'。奇怪,但如此。 –

3

要回答這個問題的稱號,

$ echo "root soft nofile 65536" >> /etc/security/limits.conf 

將在文件的末尾添加一行root soft nofile 65536

要重新啓動,在許多Linux系統,你只需要做:

$ reboot 

,並測試一個值,你可以這樣做:

if [ "`ulimit -n`" -lt "65536" ]; then 
    # do stuff 
fi 

所以最後,你的腳本會看起來像:

#!/bin/sh 
if [ "`ulimit -n`" -lt "65536" ]; then 
    file='/etc/security/limits.conf' 

    { 
     echo "root soft nofile 65536" 
     echo "root hard nofile 65536" 
     echo "soft nofile 65536" 
     echo "soft nofile 65536" 
    } >> $file 

    reboot 
fi 
+0

+1的指導和解釋 –

0

冷杉t檢查限制設定,如果是小於65536,則追加行文件的末尾,然後重新啓動

if [ `ulimit -n` -lt 65536 ];then 
cat >> /etc/security/limits.conf << EOF 
root soft nofile 65536 
root hard nofile 65536 
soft nofile 65536 
soft nofile 65536 
EOF 
reboot 
fi 
相關問題