2016-03-03 98 views
-1

有一個小bash腳本如下有沒有什麼辦法來顯示,如果火柴盒

#!/bin/bash 
C="Subsystem  sftp /usr/libexec/openssh/sftp-server #### #### " 
if [ "$D(`grep Subsystem /etc/ssh/sshd_config`)" == "$C" ] 
then 
echo "Entry is proper in sshd_config file" 
else 
echo " Entry present in conf file $D " 
echo "" 
echo -e "it should be \t\t\t$C " 
fi 

但輸出類似如下

Entry present in conf file ($D--- is missing) 

it should be  Subsystem  sftp /usr/libexec/openssh/sftp-server #### #### 

的變量有什麼辦法來顯示的$ d如果火柴盒?

+2

您從未設置過$ D? – 123

+1

我想你只是想像''$(grep Subsystem/etc/ssh/sshd_config)''執行grep。我不知道D在做什麼 –

+1

什麼是「if火柴盒」? – Jens

回答

0

我認爲這是你在試圖實現的問題。

只需將C更改爲您要搜索的字符串即可。

#!/bin/bash 

CONF_FILE="/etc/ssh/sshd_config" # the file to search 
C="Port 22"      # The string you want to find 
RESULTS=$(grep "$C" $CONF_FILE) # The grep result 
LINES_FOUND=$(echo $RESULTS | wc -l) # How many lines contain C in CONF_FILE 

if [ $LINES_FOUND -eq 0 ]; then # File does not contain C 
    echo "Entry not present in conf file $CONF_FILE" 
    echo "" 
    echo -e "it should be \t\t\t$C" 
else       # At least one line matches 
    echo "Entry is proper in $CONF_FILE" 
fi 
+0

@ crick_007 ...感謝您的努力。它工作但使用不同的方式。但是,謝謝你和所有專家的幫助。 – Sandy

相關問題