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 05-20-2011, 02:27 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
Scripting help needed


For getting individual entries from $PATH variable i m using following scrip let.
Code:
#!/bin/bash

COUNT=`echo "$PATH" | awk -F ":" '{print NF}'`
while [ "$COUNT" -ge 1 ]
do
echo "$PATH" | awk -v m=$COUNT -F ":" '{print "ls -ld " $m}'|sh
COUNT=`expr $COUNT - 1`
done
Is there any other way of doing it ?
 
Old 05-20-2011, 02:35 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Yes. You can either do anything from awk or use pure bash. The latter will look like this:
Code:
#!/bin/bash
OLD_IFS="$IFS"

IFS=":"
for dir in $PATH
do
  ls -ld $dir
done

IFS="$OLD_IFS"
This take advantage from the fact you can change the input field separator, so that the for loop iterates over the colon separated list of directories. The OLD_IFS stuff serves to save and restore the default value before and after the loop respectively.
 
Old 05-20-2011, 03:04 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Hello colucix

As a variation on the same (at the expense of using a subshell, avoids need to save and restore IFS):
Code:
( export IFS=':'; for dir in $PATH; do ls -ld $dir; done )
EDIT:

It could more simply be:
Code:
( IFS=':'; for dir in $PATH; do ls -ld $dir; done )

Last edited by catkin; 05-20-2011 at 03:05 AM.
 
1 members found this post helpful.
Old 05-20-2011, 03:15 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hello catkin!

That's a nice idea (I always hated this OLD_IFS stuff)! Something to take in mind as future reference.
 
Old 05-20-2011, 04:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
How about we just make it easy and leave IFS alone
Code:
for dir in ${PATH//:/ }; do ls -ld $dir; done
 
1 members found this post helpful.
Old 05-20-2011, 04:27 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi grail! That's right! Another nice solution.
 
Old 05-20-2011, 04:50 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Actually, I assume something else must be going to be done in the loop as the substituion could just be used with the ls on its own:
Code:
ls -ld ${PATH//:/ }
 
Old 05-20-2011, 06:15 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by grail View Post
Actually, I assume something else must be going to be done in the loop as the substituion could just be used with the ls on its own:
Code:
ls -ld ${PATH//:/ }
Neat
 
  


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
Scripting help needed. ZAMO Linux - General 2 01-12-2009 03:05 AM
Scripting help needed. ZAMO Linux - General 5 12-31-2008 06:36 AM
Scripting help needed. ZAMO Linux - General 1 09-17-2008 10:35 PM
a little scripting help needed!! vatsan1 Linux - General 6 09-02-2007 08:50 AM
Scripting help needed. stonelee Linux - Software 2 09-29-2003 09:47 AM


All times are GMT -5. The time now is 01:31 PM.

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