Help Running Loop Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Running Loop Script
# 1  
Old 05-18-2010
Help Running Loop Script

Hi

I have a file called lastlogin_1 that contains a list of usernames. I would like to put together a quick loop script that will use a variable (the contents of lastlogin_1 file) and grep all directories and sub directories in the current directory (/in/the/current/directory) for each username in the lastlogin_1 file and remove all entries of the usernames that exist in files under these directories.

The files contain information as follows: (user access to various menus)

:allow:davidw,simonk,katrinad,elizabeo,rachelm,jodiew,carolep,timj,colleenh,audreys,angelal:

The lastlogin_1 file contains entries as follows:
jamesm> more lastlogin_1
aaron
adamb
alan
alanr
alanr
alecia
aleda
alexs
alfred

Should the username be removed it will leave a double comma ,, in the file which might cause problems with other users access so I would like to remove only the comma following the user being removed.

Can someone point me in the right direction of how to do this with the use of a script. I have tried a few things but failing at the first hurdle when trying to set the variable ie

Code:
USERS=${<Lastlogin_1} or USERS= awk '{print $1}' lastlogin_1

Thanks in advance
# 2  
Old 05-18-2010
Code:
$ cat lastlogin_1
:allow:davidw,simonk,katrinad,elizabeo,rachelm,jodiew,carolep,timj,colleenh,audreys,angelal:

Code:
#!/bin/bash
USERS=$(awk -F: '{print $3}' lastlogin_1 | tr ',' '\n')
for U in $USERS
do
    echo "$U" # Just for testing purpose
    # and further processing
done

# 3  
Old 05-18-2010
A separate variable is not needed

Code:
awk -F: '{print $3}' lastlogin_1 | tr ',' '\n' | while read user
do
  echo $user
done

# 4  
Old 05-19-2010
Thanks for the responses. Using the following (changed to $1)

Code:
USERS=$(awk -F: '{print $1}' lastlogin_1 | tr ',' '\n')
for U in $USERS
do
    echo "$U" # Just for testing purpose
    # and further processing
done

outputs what is already in the lastlogin_1 file, as does

Code:
USERS=$(awk -F: '{print $1}' lastlogin_1)
for U in $USERS
do
    echo "$U" # Just for testing purpose
    # and further processing
done

I have many files in various directories and sub-directories that contain all the users in the variable $USERS. I would like to remove each user from every file they exist in under the main
directory= /apps/fourgen/accounting/menu.

The files that contain the users I want to remove have the following format

Code:
:allow:davidw,simonk,katrinad,elizabeo,rachelm,jodiew,carolep,timj,colleenh,audreys,angelal:

Now that I have the variable $USERS how do I search every file from the main directory including all sub-directories and remove all existing entries for each username within the variable.

More help appreciated.. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running in a loop in UNIX

I have a table in oracle which has 2 columns table_name table_name1 U1 T1 U2 T2 I have to take these as a variable in unix and then go to /home/bin and execute a unix command using these variables. Considering that there is only one row in the table I was able to write the below but need help... (2 Replies)
Discussion started by: Ashcalin
2 Replies

2. Shell Programming and Scripting

While loop is running only for the first iteration

Hello, I've written a script to automate encoding of all the MP4 files in a directory (incl. subdirectories). But unfortunately it's running for the first MP4 file only. My machine details: root@Ubuntu16:~# uname -a Linux Ubuntu16 4.10.0-28-generic #32~16.04.2-Ubuntu SMP Thu Jul 20 10:19:48... (2 Replies)
Discussion started by: prvnrk
2 Replies

3. Shell Programming and Scripting

Select command going to infinite loop after running the script

cd /opt/et/WAS/apps/8.0 find . -name "HostIntegration.properties" -o -name "HostSocket.properties" -o -name "environment.properties" 2> /dev/null | awk -F '' '{print $4}'|awk '!x++' | cat>/home/cbadmin/file1.txt cd /home/cbadmin/ PS3='Please enter a number from list of applications==>:' select... (3 Replies)
Discussion started by: bhas85
3 Replies

4. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

5. Shell Programming and Scripting

While Loop not running

echo "The recreate was successfully completed for device file $LCL_CLN_FILE." >> $LOG_FILE remaining=$(symclone -sid $LCL_SID -f $ETC/$LCL_CLN_FILE query | grep MB | awk '{print $2}') if then while do echo "$remaining MB's to be copied........." >> $LOG_FILE ... (10 Replies)
Discussion started by: rajsan
10 Replies

6. Shell Programming and Scripting

help in running while loop inside a shell script

I have an input file at ./$1.txt with content of seq numbers like : 1234567890 1234589700 . . so on.. I need to take each seq nbr from the input file ,run the query below: select ackname,seqnbr from event where event_text like '%seqnbr( from the input file)' and redirect the... (11 Replies)
Discussion started by: rkrish
11 Replies

7. Shell Programming and Scripting

For loop and running 2 commands at once?

HI. I am trying to run 2 commands, using the "for x in a b c d" loop but i am having a hard time coding it... Here is what i have so far: for SERVER in SERVERA SERVERB SERVERC SERVERD SERVERE do ############################################################################### #... (5 Replies)
Discussion started by: Stephan
5 Replies

8. Shell Programming and Scripting

loop when process running

Hi Gurus, Could you please help me to create a shell script that will be started by a cron job once every night at 24.00 h (that should bee easy:)) The shell script should control every 30 seconds the name of a process, and when the process doesn't run anymore it should execute a few further... (12 Replies)
Discussion started by: blackwire
12 Replies

9. Shell Programming and Scripting

Running a script in INFINITE LOOP

Hi All, I have a requirement as below. I supposed to get a file from Source system once in a month. But we dont know when the source system will send the file. My script has to wait for that file in LOOP once it gets the file then it has to FTP the file. I thought of scheduling the job... (5 Replies)
Discussion started by: Raamc
5 Replies

10. Shell Programming and Scripting

running command in while loop

I have a script that users a while read, whithin this I want to execute the cp -i command on some of the results, however because it is in a loop it skips the -i part of cp. How do I get the loop to stop each time cp -i is run and wait for the user to type the response. (2 Replies)
Discussion started by: gefa
2 Replies
Login or Register to Ask a Question