Sponsored Content
Top Forums Shell Programming and Scripting Reading ls -l output line by line awk the user name and su user to run commands Post 302872115 by zubairom on Thursday 7th of November 2013 05:00:21 PM
Old 11-07-2013
IBM Reading ls -l output line by line awk the user name and su user to run commands

Using ksh on AIX what I am trying to do is to read the ls -l output from a file in a do while loop line by line. Extract the user name(3rd field) and the directory/file name(9th field) using awk and save them into variables. su -c to the user and change directory/file permisions to 777. Script I wrote is

Code:
#!/bin/ksh

cd /saswork/sastemp
ls -ltr  | egrep -i -v 'total [0-9]+' > /u/sasp/scripts/purge_clean/test_list
 
while read line 
do
   # su the owner of each work directory and change permisions to 777 for the work directory
    
      
        sas_user=`awk '{print $3}'`
        sas_work_dir=`awk '{print $9}'`
        echo $sas_user 
         echo $sas_work_dir 
       su $sas_user -c "whoami; cd /saswork/sastemp; chmod 777 $sas_work_dir"
done < /u/sasp/scripts/purge_clean/test_list

However the problem is that it seems like that once the first awk is executed all iterations of do while are done within first awk sub shell? as it gives me all user Ids in one line (return the value in sas_user variable), output from second awk is blank as it looks like it has nothing to process? and then it tries to su all the user ids at a time and su gives error.

What I expected it to do was to awk the user Id and D/F name for first row in the ls -l output, su -c the first user to change the permisons and then go to next line of ls -l , process it the same way and keep doing it till it has read all lines from ls -l output. This is why I used a do while loop reading file line by line. If I wanted the awk to process all lines in ls -l output together I could have just piped the ls -l output to awk...

following is what I get when I run the script as root

Code:
root:/u/sasp/scripts/purge_clean]./test_su4
dsm04 dsm04 sasp omecea hbharuch hbharuch mdu vsingh dsm04 vsingh ssun sli yyao aaronwne ssun dsm01 ssun sli yyao alawson vfan vsethi vneto twang yyao vneto szhi szhi mdu
ksh: dsm04:  not found.

if I run the script as non-root user then I get

s
Code:
asp:/u/sasp/scripts/purge_clean]test_su4
dsm04 dsm04 sasp omecea hbharuch hbharuch mdu vsingh dsm04 vsingh ssun sli yyao aaronwne ssun dsm01 ssun sli yyao alawson vfan vsethi vneto twang yyao vneto szhi szhi mdu
dsm04's Password:

Any help would be greatly appreciated

Last edited by Scrutinizer; 11-07-2013 at 06:22 PM.. Reason: code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

reading ps command's output line by line

hi; as a pseudo; while read psLine do myFunc $psLine done < ps i don't want to redirect ps command's to a file. in fact, my problem is "how can i read stdout line by line in bash, sed, awk or any?" thanks, (5 Replies)
Discussion started by: s. murat
5 Replies

2. Shell Programming and Scripting

How a normal user run a script including root privileaged commands

Dear all Ihave written a script in Hpux9.0, the ecript is working fine if I run it from root command prompt But when I am running it thru /etc/profile or /user/.profile and login as a normal user, the owner of the process running the script is the normal user & hence cant run a root privileaged... (7 Replies)
Discussion started by: initin
7 Replies

3. Shell Programming and Scripting

reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt. and i am stuck. list=`cat /u/Test/programs`; psg "ServTest" | awk -v listawk=$list '{ cmd_name=($5 ~ /^/)? $9:$8 for(pgmname in listawk) ... (6 Replies)
Discussion started by: Anteus
6 Replies

4. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

5. Shell Programming and Scripting

How to take input from the user from the command line and execute commands basedon that?

Hi, I am using solaris 10 and bash shell.Script execution follows below.Initially it will check whether a directory exists or not if does not exist it will create it.(This I have completed) Second step:I have four users say user1,user2,user3,user4.Script should prompt for the user id and... (11 Replies)
Discussion started by: muraliinfy04
11 Replies

6. Shell Programming and Scripting

how to run a command line with another user without prompts for password

Hi, I'm writing a script, in the script I need to use tcpdump to capture some packets however it needs root priviledge my computer is configured by school and I have no real root priviledge so I can't use sudo on my computer,like Code: sudo tcpdump ...... I have to use a limited... (1 Reply)
Discussion started by: esolve
1 Replies

7. Shell Programming and Scripting

Script to run commands as root user

Hello I have a script which is working fine so far to generate HTML file. Now i am wondering how do i include a syntax where it can change itself to root user and execute a specific commands as root user. Please help, Thanks in advance. -Siddhesh (2 Replies)
Discussion started by: Siddheshk
2 Replies

8. AIX

List of AIX commands that can be run by ROOT user ONLY

Hello, I am testing sudo and I want to test it. Can anyone please let me know few commands (of course other than shutdown, reboot etc. as I can't reboot the box) on AIX that can be run by ROOT only. Thanks ---------- Post updated at 07:43 PM ---------- Previous update was at 07:38 PM... (5 Replies)
Discussion started by: prvnrk
5 Replies

9. HP-UX

How capture all user command line output?

Hi I want to know how capture all user command line output and save this commands and outputs to text files? if you have script for this subject please give me.:o please help me thank you (6 Replies)
Discussion started by: amvhd
6 Replies

10. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies
JOIN(1) 						      General Commands Manual							   JOIN(1)

NAME
join - relational database operator SYNOPSIS
join [ options ] file1 file2 DESCRIPTION
Join forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. If one of the file names is the standard input is used. File1 and file2 must be sorted in increasing ASCII collating sequence on the fields on which they are to be joined, normally the first in each line. There is one line in the output for each pair of lines in file1 and file2 that have identical join fields. The output line normally con- sists of the common field, then the rest of the line from file1, then the rest of the line from file2. Input fields are normally separated spaces or tabs; output fields by space. In this case, multiple separators count as one, and leading separators are discarded. The following options are recognized, with POSIX syntax. -a n In addition to the normal output, produce a line for each unpairable line in file n, where n is 1 or 2. -v n Like -a, omitting output for paired lines. -e s Replace empty output fields by string s. -1 m -2 m Join on the mth field of file1 or file2. -jn m Archaic equivalent for -n m. -ofields Each output line comprises the designated fields. The comma-separated field designators are either 0, meaning the join field, or have the form n.m, where n is a file number and m is a field number. Archaic usage allows separate arguments for field designators. -tc Use character c as the only separator (tab character) on input and output. Every appearance of c in a line is significant. EXAMPLES
sort /adm/users | join -t: -a 1 -e "" - bdays Add birthdays to password information, leaving unknown birthdays empty. The layout of is given in users(6); bdays contains sorted lines like tr : ' ' </adm/users | sort -k 3 3 >temp join -1 3 -2 3 -o 1.1,2.1 temp temp | awk '$1 < $2' Print all pairs of users with identical userids. SOURCE
/sys/src/cmd/join.c SEE ALSO
sort(1), comm(1), awk(1) BUGS
With default field separation, the collating sequence is that of sort -b -ky,y; with -t, the sequence is that of sort -tx -ky,y. One of the files must be randomly accessible. JOIN(1)
All times are GMT -4. The time now is 06:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy