awk in ssh in su in a command substitution

SpruceTips

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?

muru

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

command substitution inside awk

From Dev

Awk substitution within a find command

From Dev

ssh in for loop and excute command as su

From Dev

Escape awk command in ssh

From Dev

Does awk -v accept command substitution?

From Dev

Converting vim substitution with back references to sed, awk or similar unix command

From Dev

why is a double-quoted awk command substitution failing in csh

From Dev

Command substitution with string substitution

From Dev

Backticks with su -c in SSH Command without running command in it

From Dev

Pass commands as input to another command (su, ssh, sh, etc)

From Dev

I Can't login as root with su command, but I can with SSH

From Dev

How to execute 'su' command using parallel-ssh

From Dev

AWK multiple patterns substitution

From Dev

Conditional string substitution in awk

From Dev

awk parameter substitution fails

From Dev

remote SSH and variable substitution

From Dev

Execute a remote ssh command as root user supplying su password in command line

From Dev

variable expansion within command substitution over SSH bash 4.X

From Dev

escaping awk with remote ssh command and bash that is already escaped

From Dev

Shell variable substitution in command

From Dev

Bash Command Substitution to an Array

From Dev

Linux command variable substitution

From Dev

Sed substitution and external command

From Dev

Delimited command substitution in Fish

From Dev

Command Substitution in Bash for tty

From Dev

Command substitution and $PATH variable

From Dev

Command substitution in for loop not working

From Dev

execute a command if substitution succeded

From Dev

Command substitution alters formatting

Related Related

HotTag

Archive