Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

grep a variable

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-01-2008
sgstuart sgstuart is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 2 October 2008, 12:11 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
grep a variable

Hi all,
I am trying to do a simple thing in my mind. However I am fairly new to bash. What I need to do is create a folder for each partition on each CD, and each partition has a unique name (with spaces in it, do not ask why, it is already done Linux ) . All CD's will show up under disk1s. So what I have done is create a script that will grab the name, assuming that there is only 1 partition (which I never knew anyone would create partitions on a CD).

So my script write now is

if [-z `diskutil list | grep disk1s0 | awk '{print$3}' ] ; then
Name = `diskutil list | grep disk1s1s2 | awk '{print$3}'
T1 = "Mass"
if[$T1 = $Name]; then
echo `diskutil list | grep disk1s1s2 | awk '{print$3 " " $4}'
else
echo `diskutil list | grep disk1s1s2 | awk '{print$3}'
fi
else
echo `diskutil list | grep disk1s0 | awk '{print$3}'
fi


what I need to add is if the disk1s1s2 name starts with Mass is I need it to look at the name of possibily multiple partitions
echo `diskutil list | grep disk1s1s2 | awk '{print$3 " " $4}'
echo `diskutil list | grep disk1s2s2 | awk '{print$3 " " $4}'
echo `diskutil list | grep disk1s3s2 | awk '{print$3 " " $4}'
echo `diskutil list | grep disk1s4s2 | awk '{print$3 " " $4}'
echo `diskutil list | grep disk1s5s2 | awk '{print$3 " " $4}'
and so on, I have seen at least 10, but there could be more, and could be less.
How would I script for this? Any help would be great.

Thanks,
Steven Stuart
Sponsored Links
    #2  
Old Unix and Linux 10-01-2008
sgstuart sgstuart is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 2 October 2008, 12:11 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
while loop is all I need now

Hi all,
now all I need is to make my while loop to work, which might be syntax.

So I have

i=1
while[-n `diskutil list | grep disk1s"$i"s2 | awk '{print$3}' ]; do
echo `diskutil list | grep disk1s"$i"s2 | awk '{print$3 " " $4}'
i+=1
done

If I remove the while loop portion just keeping the i= and the echo line it works perfectly, If I make the i=2 and so forth it works perfectly, so I know that my while loop is not correct.

Thanks,
Steven Stuart
Sponsored Links
    #3  
Old Unix and Linux 10-02-2008
sgstuart sgstuart is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 2 October 2008, 12:11 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Bug solved

I created a new variable and different syntax was needed

i=1
next=`diskutil list | grep disk1s"$i"s2 | awk '{print$3}'
while[ $next = $T1 ]
do
echo `diskutil list | grep disk1s"$i"s2 | awk '{print$3 " " $4}'
i=$(( $i + 1 ))
next=`diskutil list | grep disk1s"$i"s2 | awk '{print$3}'

done

Now I get the correct results that I wanted. The section above gets placed in place of the original

echo `diskutil list | grep disk1s1s2 | awk '{print$3 " " $4}'

line

Thanks,
Steven Stuart
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep through a variable rajesh Shell Programming and Scripting 6 10-13-2011 06:43 AM
grep using variable BeefStu Shell Programming and Scripting 3 09-03-2011 12:36 PM
grep a variable lassimanji Shell Programming and Scripting 9 04-27-2009 02:35 PM
grep with variable ozvena Shell Programming and Scripting 5 05-14-2008 03:05 PM
How to grep a variable? whatisthis Shell Programming and Scripting 2 09-14-2004 06:26 PM



All times are GMT -4. The time now is 12:07 AM.