1

Good day All,

I'm attempting to run a for loop via SSH. The loop is used to get the last login of each user that has an account on the server,

ssh 172.20.2.1 '''for i in `cat /etc/passwd | awk -F: '{print $1}'`; do last -n 1 $i | sed -e '/^$/{N;d;}'; done'''

I am getting the error,

    bash: -c: line 1: syntax error: unexpected end of file
   -bash: d: command not found
   -bash: }; done: command not found

I Googled but didn't get much to go on.

Any assistance would be appreciated.

3

The error is because you are using ''' to quote your command. Since you are also using single ' to quote your awk and sed commands, the shell thinks the command is over and gets confused. As far as I know, there is no such thing as '''; the first two ' are ignored since they simply define an empty string and the last is taken as an opening quote which is closed by the opening quote of your awk. You can see this if you look at how your code is highlighted in your question.

The simplest solution is to use single quotes to enclose the command and double quotes for the sed and awk. However, you will then need to escape the $1 in the awk script so that it's not interpreted as a shell variable. You're also complicating things for no reason. All you really need is:

ssh localhost 'cut -d: -f1 /etc/passwd | 
    while read i; do last -n 1 $i | sed -e "/^$/{N;d;}"; done'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.