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
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
if I run the script as non-root user then I get
s
Any help would be greatly appreciated
Last edited by Scrutinizer; 11-07-2013 at 06:22 PM..
Reason: code tags
You might have better luck with a much simpler approach:
Note, however, that your original script and this replacement will both fail miserably if there are any whitespace characters in any of the filenames in /saswork/sastemp. Both will also change any files found; not just directories.
This User Gave Thanks to Don Cragun For This Post:
The script you gave did the job. I am not concerned about the blank spacees in dir/file names because in my final script I filter the ls -l output using egrep, to keep only the directories that match a pattern, before I would read it in the do while loop to do su.
The script you gave did the job. I am not concerned about the blank spacees in dir/file names because in my final script I filter the ls -l output using egrep, to keep only the directories that match a pattern, before I would read it in the do while loop to do su.
Thanks for your help
Note that I replaced the egrep in your pipeline with a simple if statement inside the loop. The only thing your egrep was doing was to remove the line in the ls output that is of the form:
Nothing in our scripts verify that there are no spaces, tabs, or newlines in the names of the files in the directory. Hopefully, it won't be an issue because you won't create filenames that contain these characters. Scripts can be written to handle such filenames, but it is MUCH SIMPLER for everyone involved if you just don't create filenames that need special handling.
Actually the script I showed was only a part of a big script. In the main script I have another egrep filter to keep only the directories that match the pattern of SAS work directories, in the ls -l output. This is why I know that there won't be any directories with blanks in their name, in the ls -l output I am going to use in do while loop.
I did not show the complete script because I did not want to lose focus from my issue which was how to extract each owner id and su to the user to change permisions, which was solved by your script.
I agree with you that creating files/directories in UNIX with blanks is a very bad idea.... but from time ot time we do see users who do that.
The plain fix that CarloM means
Otherwise awk reads from stdin i.e. the rest of the loop's input!
The read into individual variables is much better of course.
The su remains problematic. I suggest the following safety enhancements
</dev/null prevents an eventual reading from stdin.
-f skips the user's .bashrc .kshrc .cshrc processing.
The 'ticks' can handle special characters in the file name. NB it must be 'ticks' here because it is already within "quotes".
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)
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)
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)
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)
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)
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)
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)
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)
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)