LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   AIX (https://www.linuxquestions.org/questions/aix-43/)
-   -   building an array in sh on AIX (https://www.linuxquestions.org/questions/aix-43/building-an-array-in-sh-on-aix-4175659081/)

unix1adm 08-13-2019 01:32 PM

building an array in sh on AIX
 
I have a script that works fin in Linux Specifically Red hat.

However when i go to run it in AIX it gets an error

Not sure if this is an active forum or the correct place to put this question.

Any help is appreciated.



# Retrieve and format the system date/time appropriately
datetime=$(date "+%D %r")

# Enumerate users and non-empty groups on the system
for username in $(cat /etc/passwd | awk '/^#/{next}1' | cut -d: -f1); do
# Search for users in primary groups and append to array
userprimarygroupid=$(grep ^$username /etc/passwd | awk '/^#/{next}1' | cut -d: -f4)
userprimarygroupname=$(grep ":$userprimarygroupid:" /etc/group | awk '/^#/{next}1' | cut -d: -f1)
outputarr+=( ""$userprimarygroupname"",""$username"",""Local User"",""$datetime"")
# Search for users in secondary groups and append to array
for usersecondarygroupname in $(grep $username /etc/group | cut -d: -f1); do
outputarr+=( ""$usersecondarygroupname"",""$username"",""Local User"",""$datetime"")
done
done

# Write Results to stdout for mgmt systems to retrieve
printf '%s\n' "${outputarr[@]}" | sort -u


---------------------

error

./localgroup.sh[6]: 0403-057 Syntax error at line 10 : `(' is not expected.

TB0ne 08-13-2019 01:51 PM

Quote:

Originally Posted by unix1adm (Post 6024766)
I have a script that works fin in Linux Specifically Red hat.
However when i go to run it in AIX it gets an error Not sure if this is an active forum or the correct place to put this question. Any help is appreciated.
Code:

# Retrieve and format the system date/time appropriately
datetime=$(date "+%D %r")

# Enumerate users and non-empty groups on the system
for username in $(cat /etc/passwd | awk '/^#/{next}1' | cut -d: -f1); do
        # Search for users in primary groups and append to array
        userprimarygroupid=$(grep ^$username /etc/passwd | awk '/^#/{next}1' | cut -d: -f4)
        userprimarygroupname=$(grep ":$userprimarygroupid:" /etc/group | awk '/^#/{next}1' | cut -d: -f1)
        outputarr+=( ""$userprimarygroupname"",""$username"",""Local User"",""$datetime"")
  # Search for users in secondary groups and append to array
  for usersecondarygroupname in $(grep $username /etc/group | cut -d: -f1); do
                        outputarr+=( ""$usersecondarygroupname"",""$username"",""Local User"",""$datetime"")
  done
done

# Write Results to stdout for mgmt systems to retrieve
printf '%s\n' "${outputarr[@]}" | sort -u

error
Code:

./localgroup.sh[6]: 0403-057 Syntax error at line 10 : `(' is not expected.

Since you didn't post the whole script, what shell does this run in? Bash? KSH? CSH? ZSH? ASH? Other?? What is on line 10, since it's telling you that's where the syntax error is? Have you tried to step through and run the commands in this script on the command line, to see what results you get? Output from things like the passwd file, grep, and other system-utilities may be different on AIX than they are on Linux. I'd start there.

unix1adm 08-13-2019 02:22 PM

Sorry this is the whole script. I only forgot the #/bin/sh line.

#!/bin/sh

TB0ne 08-13-2019 02:44 PM

Follow up in your DUPLICATE thread, thanks.

unix1adm 08-13-2019 02:53 PM

I sincerely apologizes for ruining your day and raising your blood pressure by my fopar in posting to this site wrongly.

Thank for your time.

scasey 08-13-2019 03:50 PM

Your dup thread got closed. Please respond here to the questions I asked there...

MadeInGermany 08-14-2019 01:19 PM

The script needs bash. It only works with /bin/sh if /bin/sh is a link to bash.

Michael AM 08-28-2019 04:12 AM

Does it need bash because of the "+=" operator?

If yes, what would be the best syntax using ksh, or ksh93?

If no, please ignore - looking for my rock to hide under :p

Firerat 08-28-2019 06:24 AM

Quote:

Originally Posted by unix1adm (Post 6024766)
Code:

for username in $(cat /etc/passwd | awk '/^#/{next}1' | cut -d: -f1); do

makes me cringe

you should re-write the whole script
no need to cat , awk will read the file just fine
cut the output of awk?
why not just get awk to process the input correctly?

MadeInGermany 08-31-2019 09:41 AM

Quote:

Originally Posted by Michael AM (Post 6030473)
Does it need bash because of the "+=" operator?

If yes, what would be the best syntax using ksh, or ksh93?

If no, please ignore - looking for my rock to hide under :p

Yes, ksh might work with
Code:

outputarr=("${outputarr[@]}" ...)
instead of
Code:

outputarr+=(...)
And in AIX, /bin/sh might be a link to ksh.

MadeInGermany 08-31-2019 10:03 AM

Quote:

Originally Posted by Firerat (Post 6030506)
makes me cringe

you should re-write the whole script
no need to cat , awk will read the file just fine
cut the output of awk?
why not just get awk to process the input correctly?

Use a while loop for passwd and awk for group
Code:

while IFS=":" read username pw uid guserprimarygroupid rest
do
  case $username in
  (#*)
    echo "skipping valid! username '$username'"
    continue
  ;;
  esac
  userprimarygroupname=$(
    awk -F":" -v gid="$userprimarygroupid" '$3==gid { print $1; exit }' /etc/group
  )
  ...
done < /etc/passwd


Firerat 08-31-2019 10:11 AM

and that should work in other shells
no bashisms in it


All times are GMT -5. The time now is 02:42 AM.