LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices


Reply
  Search this Thread
Old 08-13-2019, 01:32 PM   #1
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Rep: Reputation: 32
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.
 
Old 08-13-2019, 01:51 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,820

Rep: Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774
Quote:
Originally Posted by unix1adm View Post
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.
 
Old 08-13-2019, 02:22 PM   #3
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Original Poster
Rep: Reputation: 32
Sorry this is the whole script. I only forgot the #/bin/sh line.

#!/bin/sh
 
Old 08-13-2019, 02:44 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,820

Rep: Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774Reputation: 5774
Follow up in your DUPLICATE thread, thanks.
 
Old 08-13-2019, 02:53 PM   #5
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Original Poster
Rep: Reputation: 32
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.
 
Old 08-13-2019, 03:50 PM   #6
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,630

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
Your dup thread got closed. Please respond here to the questions I asked there...
 
Old 08-14-2019, 01:19 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,178

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
The script needs bash. It only works with /bin/sh if /bin/sh is a link to bash.
 
Old 08-28-2019, 04:12 AM   #8
Michael AM
Member
 
Registered: May 2006
Distribution: AIX 5.3, AIX 6.1, AIX 7.1
Posts: 121

Rep: Reputation: 33
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
 
Old 08-28-2019, 06:24 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,964

Rep: Reputation: 539Reputation: 539Reputation: 539Reputation: 539Reputation: 539Reputation: 539
Quote:
Originally Posted by unix1adm View Post
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?

Last edited by Firerat; 08-28-2019 at 06:31 AM.
 
Old 08-31-2019, 09:41 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,178

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
Quote:
Originally Posted by Michael AM View Post
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
Yes, ksh might work with
Code:
outputarr=("${outputarr[@]}" ...)
instead of
Code:
outputarr+=(...)
And in AIX, /bin/sh might be a link to ksh.
 
Old 08-31-2019, 10:03 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,178

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
Quote:
Originally Posted by Firerat View Post
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
 
1 members found this post helpful.
Old 08-31-2019, 10:11 AM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,964

Rep: Reputation: 539Reputation: 539Reputation: 539Reputation: 539Reputation: 539Reputation: 539
and that should work in other shells
no bashisms in it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Aix 5 Aix 5l ?! jamesps AIX 3 01-21-2005 11:55 AM
System hangs in update of AIX from 4.3.3 to AIX 5.2 jmurray67 AIX 2 07-25-2004 08:25 PM

LinuxQuestions.org > Forums > Other *NIX Forums > AIX

All times are GMT -5. The time now is 08:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration