0

I have the following script:

while [ "$1" != "" ]
do
case "$1" in
    -h) HOST="$2"; shift 2;;
esac
done

if test -z "$HOST"
then
    [...]
else
    TODAY=$(date +'%Y-%m-%d') 
    configs=("/etc/inittab" "/etc/default/cron" "/etc/default/login" "/etc/default/su" "/etc/group" "/etc/inet/inetd.conf" "/etc/mnttab" "/etc/netmasks" "/etc/nsswitch.conf" "/etc/protocols" "/etc/services" "/etc/syslog.conf" "/var/sadm/install/contents" "/etc/user_attr" "/etc/passwd")  
    ssh $HOST 
    for (i=0; i<${#configs[*]}; i++) 
    do 
        digest -v -a md5 ${configs[$i]} | awk '{print $2,$4}' | sed 's/) (/\n/;s/[()]//g'; 
    done > /tmp/md5_config_$TODAY.txt
fi

If I execute the script with a hostname (scriptname.sh -h hostname) I get the error

line 97: syntax error near unexpected token `('
line 97: `for (i=0;i<${#configs[*]};i++;); '

Is there a way to solve it? I want to execute the "digest" command on the remote host with the filenames of the array "configs"

Edit: Actual state

else
    TODAY=$(date +'%Y-%m-%d') 
    configs=("/etc/inittab" "/etc/default/cron" "/etc/default/login" "/etc/default/su" "/etc/group" "/etc/inet/inetd.conf" "/etc/mnttab" "/etc/netmasks" "/etc/nsswitch.conf" "/etc/protocols" "/etc/services" "/etc/syslog.conf" "/var/sadm/install/contents" "/etc/user_attr" "/etc/passwd")  
    ssh $HOST 
    for ((i=0; i<${#configs[*]}; i++)) 
    do 
        digest -v -a md5 ${configs[$i]} | awk '{print $2,$4}' | sed 's/) (/\n/;s/[()]//g'; 
    done > /tmp/md5_config_$TODAY.txt
fi
8
  • 2
    Remove the semicolon near the i++ in the line 97. Commented Mar 25, 2014 at 10:36
  • I removed it (Code: for (i=0;i<${#configs[*]};i++); ) but still got the same error Commented Mar 25, 2014 at 10:39
  • Avinash means : remove the ; at the end of the line for(i=0.....) Commented Mar 25, 2014 at 10:39
  • 1
    The bash for loop has a very specific syntax. You need double parentheses around it, semicolons between the three expressions but not after the last, and also no semicolon after the closing parenthesis. Both of your loops contain syntax errors. Is this really the script, or did you type it in manually? Commented Mar 25, 2014 at 10:42
  • Thank you @OlivierDulac. Now i got the little problem that the ssh command is working but i end up on bash of remote host without execution of the lines which follow the "ssh $HOST" command Commented Mar 25, 2014 at 10:42

1 Answer 1

1

If I was to write such a thing I would

TODAY=`date --iso`
CONFIGS="/etc/inittab /etc/default/cron /etc/default/login ..."

ssh -q $HOST 'for cfg in '${CONFIGS}'; do digest -v -a md5 ${cfg}; done' |\
    awk '{print $2,$4}' | sed 's/) (/\n/;s/[()]//g' >> /tmp/md5_config_$TODAY.txt

However, you might want to check out Tripwire and other tools, which are made for such purposes.

1
  • Thank you very much for the help Bananguin. The script works as it should Commented Mar 25, 2014 at 13:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.