2

I am creating a script that will ssh to a host and print all the user accounts and when they will expire.

On a host I can run awk -F':' '{ print $1}' /etc/passwd and it will give me a list of all user accounts.

I have added this to a script that should go to a server, create this list and use it to print when it will expie.

#!/bin/bash

for i in `cat /admin/lists/testlist`
do
  echo $i
  UNAME=`su - admin -c "ssh $i uname"`

  if test "$UNAME" = "Linux"
  then
    LIST=`su - admin -c "ssh $i awk -F':' '{ print $1}' /etc/passwd"`
    for j in $LIST
    do
      echo "$j " ; `su - batch -c "ssh $i sudo chage -l $j | grep Account"`
    done

  else
    echo "Exiting. The OS type is not found."
  fi

  echo "========================================================================"
  echo " "
done

exit 0

The issue I am having is when I run the script I get the following error.

[admin@testserver bin]$ sudo checkPasswdExpiration.sh
testserver02
awk: cmd. line:1: {
awk: cmd. line:1:  ^ unexpected newline or end of string
========================================================================

Why does the awk command not work in this script?

4

The first set of quotes is eaten up by the command line for su, and the second set by the command line for ssh, so that the quoted { print $1} is actually seen as three separate arguments by awk. Escape the quotes (and $, and any other special character you may use):

su - admin -c "ssh $i awk -F: \'{ print \$1}\' /etc/passwd"

Or:

su - admin -c "ssh $i getent passwd" | awk -F: '{print $1}'

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.