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?
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}'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments