LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2014, 01:19 PM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
Bash script help


Hi All,
I need your kind help. I have directories as below
Code:
# ls -rt1 /sometest/
lost+found
amar
akbar
antony
gauri
seema
suhas
syed
zibir
jakob
vinod
Now I just wanted to exclude below users from search result and wanted to print rest of all users
Code:
lost+found
antony
seema
vinod
So I tried
Code:
for i in /sometest/*; do echo $i | awk -F '/' '{ print $NF }'; done | egrep -v "(lost+found|antony|seema|vinod)"
But still lost+found is there in the search result.
The same way I wanted to exclude them by creating the array, let say antony, seema and vinod are the managers and I tried to create an array for them as below
Code:
MANAGER[0]="antony"
MANAGER[1]="seema"
MANAGER[2]="vinod"
for i in /sometest/*; do echo $i | awk -F '/' '{ print $NF }'; done | egrep -v "(lost+found|${MANAGER[i]})"
Got the below error.
bash: /sometest/zibir: syntax error: operand expected (error token is "/sometest/zibir")
So expecting your kind help.
 
Old 09-25-2014, 01:23 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
You want to loop on the ls don't you?

Code:
for i in /sometest/*
should be:

Code:
for i in $(ls -rt1 /sometest/*)
but really,. this would work without the complication of the for loop and awk...:

Code:
find /sometest/ | egrep -v "(lost\+found|antony|seema|vinod)"

Last edited by szboardstretcher; 09-25-2014 at 01:36 PM.
 
1 members found this post helpful.
Old 09-25-2014, 01:34 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
'+' is special in regexp. You need to escape it if you want it non-special.
 
2 members found this post helpful.
Old 09-25-2014, 10:27 PM   #4
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Thanks to both, This is what I was expecting.
Code:
for i in $(ls -rt1 /sometest); do echo $i; done | egrep -v "(lost\+found|antony|seema|vinod)"
But instead of adding each managers name with "egrep -v" command, is it possible to do the same with ${MANAGER[i]} as this variable has already set as below.
Code:
MANAGER[0]="antony"
MANAGER[1]="seema"
MANAGER[2]="vinod"
Expecting your kind help.

Last edited by cli; 09-25-2014 at 10:29 PM.
 
Old 09-26-2014, 02:52 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
probably this:
Code:
IFS='|'
ls -rt1 /sometest | egrep -v "(lost\+found|${MANAGER[*]})"
 
1 members found this post helpful.
Old 09-26-2014, 01:35 PM   #6
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Dear pan64,
First of all, sorry for the delay in reply.
May I know what IFS='|' is doing here, realy this is an excellant variable and please let me know when we should use this?
Code:
IFS='|'
ls -rt1 /sometest | egrep -v "(lost\+found|${MANAGER[*]})"
Expecting your kind reply.
 
Old 09-27-2014, 12:18 PM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
it is described on the man page of bash. that is the Internal Field Separator.
 
  


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
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 03:57 PM
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 02:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 09:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM


All times are GMT -5. The time now is 08:37 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