LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-09-2003, 04:28 PM   #1
Thetargos
Senior Member
 
Registered: Mar 2003
Location: Mexico City
Distribution: Fedora, Ubuntu & Mint
Posts: 1,679

Rep: Reputation: 45
BASH variables evaluation question.


Hi all, I have a problem... I am trying to write a script and need a way to sort out the "value" asigned to a variable so it can be then evaluated. I am not very good at describing, so I'll write what I have so far and ask you about what I actually want later:

Code:
#!/bin/bash
echo "Kernel vesion?"
read KERNVER

---conflicting portion follows----

#Find out if the sources are in place

LIST=`ls /usr/src | grep $KERNVER`

----Rest of the code goes here---
If leave the LIST variable have the value from the grep command, it will have as its value the whole output form grep, right? so I end up with something like this in my system (assuming I exported the above result of the ls|grep):
Code:
$ echo $LIST
LIST=linux-2.4 linux-2.4.20-18.9 linux-2.4.21 linux-2.4.21-ck1
linux-2.4.21-ck1.patch linux-2.4.21-SuMo linux-2.4.21.tar.bz2
supermount-1.2.8-2.4.21.patch
$
Now, my question is: How do I retrieve the space separated values from LIST? is it as valid as $1, $2, etc?

Help, I'm desperate!

PS: It occured to me that I would do it if I asigned the value to LIST if I previously had written a file with the grep command output and then evaluated that file with lets say awk or gawk... What do you think?

Last edited by Thetargos; 08-09-2003 at 04:33 PM.
 
Old 08-09-2003, 05:00 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
you could use a bash for loop to pick out each individually.

for i in $LIST; do echo $i; done

to use awk theres no need to write the output to a file just

echo $LIST | awk [options]
 
Old 08-09-2003, 05:38 PM   #3
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
here's my crappy example :

Code:
#!/bin/bash

SRCROOT="/usr/src"
KERNVER=`uname -r`
echo "Your current kernel version is : ${KERNVER}"

LIST=`ls ${SRCROOT} | grep ${KERNVER}`
echo "These files are in ${SRCROOT} : "

for EACHFILE in ${LIST}; do echo ${EACHFILE}; done
[EDIT]
the values stored in ${LIST} are actualy newline "\n" seperated, i know it looks like a space but it's not
[/EDIT]

[EDIT]
modified substantially , i did it the long way, mr. kev82's way is much cleaner ;)
(i was posting while he was...and he won ;)
[/EDIT]

Last edited by jhorvath; 08-09-2003 at 10:50 PM.
 
Old 08-10-2003, 01:15 AM   #4
Thetargos
Senior Member
 
Registered: Mar 2003
Location: Mexico City
Distribution: Fedora, Ubuntu & Mint
Posts: 1,679

Original Poster
Rep: Reputation: 45
Thank you guys!! I was also thinking about using the -o option to the grep command so that it would just fit the exact pattern I was looking for (though the "for" options looks GREAT!) like this:
Code:
#!/bin/bash
#script to test if a kernel source tree exists
KERNVER="linux-$1"
if [ "$KERNVER" = "" ]
   while :
   do
       echo "You did not supply a kernel version as an argument"
       echo "What kernel version would you like to check for?"
       read KERNVER
   done
fi

LIST=`ls /usr/src | grep -o $KERNVER`
if [ "$LIST" = "" ] 
   then
         echo "That version of kernel has no sources installed"
         exit 1
    elif [ "$LIST" != "" ]
         then
               echo "The sources wer found at /usr/src/$LIST"
fi
I must admit that the "for" loop solution looks more promising, thank you guys again.

[/edit]
PS: Pardon my bad programing style... just a newb here

Last edited by Thetargos; 08-10-2003 at 01:18 AM.
 
Old 08-10-2003, 02:02 AM   #5
Thetargos
Senior Member
 
Registered: Mar 2003
Location: Mexico City
Distribution: Fedora, Ubuntu & Mint
Posts: 1,679

Original Poster
Rep: Reputation: 45
Another rather dumb question (I haven't seen any case of it, but also haven't seen that it is not permited) is it possible to have nested "case" switches (as in C switches)?

[/edit]
When I finish the script I am working on (and proved that it works correctly ) I'll post it here

Thanks for your patience.

Last edited by Thetargos; 08-10-2003 at 02:04 AM.
 
Old 08-10-2003, 06:02 AM   #6
Thetargos
Senior Member
 
Registered: Mar 2003
Location: Mexico City
Distribution: Fedora, Ubuntu & Mint
Posts: 1,679

Original Poster
Rep: Reputation: 45
Ok my report so far... I found out that indeed I can have nested cases, and the "for" loop solution is elegant, not to say excelent. I don't wanna post all of the progress I am making on this script... but I'll post what the final solution was to the LIST problem... I think it is elegant and efficient (again my programming style may offend some, I know I'm like a brute at this ) Well here it is:

Code:
#!/bin/bash
#Test script for looking up Kernel sources.

KERNVER="linux-$1"
KERNSRC="/usr/src"

if [ "$KERNVER" = "" ]
then
	while :
	do
		echo "You did not supply any kernel version as an argument"
		echo "Enter a kernel version you'd like to look up"
		read KERNEL
		KERNVER="linux-$KERNEL"
	done
fi

# Find out whether or not the sources for the linux kernel version supplied exist
# on /usr/src, otherwise prompt for the location of the source tree.

LIST=`ls /usr/src | grep $KERNVER`
for i in $LIST
do
	if [ "$i" = "$KERNVER" ]
	then
		echo
		echo "The kernel version you entered is on ${KERNSRC}/${KERNVER}"
		break
	elif [ "$i" != "$KERNVER" ]
	then
		echo "The kernel version you supplied does not exist at $KERNSRC"
		echo "Please enter the path to the source tree"
		read KERNSRC
	fi
done

KERNSRC="${KERNSRC}/${KERNVER}"

# Test if the given path is a directory and if it is writable and accesible 	
# by the current user.

if [ ! -d "$KERNSRC" ] 
then
	echo "The kernel version you entered is not a directory!"
	exit 1
elif [ ! -x "$KERNSRC" ]
then
	echo "The kernel source tree is not accesible by user $USER."
	echo "Please change the permissions on the directory or run as root"
	exit 2
elif [ ! -w "$KERNSRC" ]
then
	echo "The kernel source tree is not writeable by user $USER."
	echo "Please change the permissions on the directory or run as root"
	exit 3
fi
What do you think of this solution?? I still need to test it intensively so to make sure it is as bugfree code as possible, but I think I'm improving...

Again, thanks guys your help was (both examples, actually) INVALUABLE.

Here I go again with a (I think) rather dumb question... Is it possible to have script run a program, for instance a network configuratoin tool, and as soon as you exit the application opened by the script, the control was returned to the script subshell? If so... (I can imagine it would be QUITE hard to accomplish) where can I find information about that?

Last edited by Thetargos; 08-10-2003 at 06:12 AM.
 
Old 08-10-2003, 01:24 PM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
Yes you can have nested case.
Code:
#!/bin/bash
#Endless loop
while :
do
      echo; echo "Hit a key, then hit return."
      read Keypress

      case "$Keypress" in
        [a-z]   ) echo "Lowercase letter";;
        [A-Z]   ) echo "Uppercase letter";;
        [0-9]   ) echo "Digit";;
        *       ) echo "Punctuation, whitespace, or other"
       #Here is an embedded case
        case "$Keypress" in
	!	) echo "Exclamation mark!";;
	,	) echo "Comma";;
	.	) echo "See ya, wouldn't wanna be ya."
		exit 0;;
	*	) echo "You got me.  I cant figure it out?";;
         esac;;
      esac
done  

exit 0
To have a script execute a program just put it in the script like you would type it on the command line.

Last edited by /bin/bash; 08-10-2003 at 01:25 PM.
 
Old 08-10-2003, 03:36 PM   #8
itzamna
LQ Newbie
 
Registered: Jul 2003
Location: Houston
Distribution: Red Hat 9
Posts: 10

Rep: Reputation: 0
as a newbie and new at this site, i'm not sure if this question goes here, you may guide me to look into a better forum for my question:
i'm trying to learn more options for the command 'cp'; if i have 100 files called from FILE_001.ABC to FILE_100.ABC and i want to copy'em to anotherfile_001.abc - anotherfile_100.abc, how could i do it withoug repeating 100 times 'cp'?
i thought it could be something like
% cp FILE_[001-100].ABC anotherfile_[001-100].abc
but looks like i really need to know more about what i'm doing here;
also, does somebod know a site with good examples of all this sort of bash tricks? i find 'man' and 'info' quit hard to follow;
thank you!
 
Old 08-10-2003, 05:06 PM   #9
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
Something like this.

cp FILE_[0-1][0-9][0-9].ABC
However this will copy from 000-199
cp FILE_0[0-9][0-9].ABC
This copies 000-099 then you just need to add 100

The thing is when you copy multiple files you have to specify a directory as the destination. To rename the files you need to loop through a list.

something like:
FILE=`ls FILE_0[0-9][0-9].ABC`
for i in $FILE; do
cp $i `echo $i|sed -e's/FILE/anotherfile/' -e's/ABC/abc/'`
done
 
Old 08-10-2003, 05:12 PM   #10
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
Go to www.tldp.org and you will find the Advanced Bash-Scripting Guide.
 
Old 08-10-2003, 05:42 PM   #11
Thetargos
Senior Member
 
Registered: Mar 2003
Location: Mexico City
Distribution: Fedora, Ubuntu & Mint
Posts: 1,679

Original Poster
Rep: Reputation: 45
/bin/bash:

What I meant by asking about the calling up applications on scripts, was because I would like a script I'm working on to call up an application (Mozilla if you really wanna know) and then if ever the application is terminated, instead of returning control to the shell on which the script was executed in the first place, it would return control to the scritp so it can take action up on exit of that program (whether it be to restart Mozilla or to close the session, etc, depending on the exit status of the called program). I know this would not be easy to program, but I would like to know where could I get information about this kind of BASH script programming (ok scripting... I know scripting is not programming ).
 
Old 08-11-2003, 05:11 AM   #12
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
OK, then just type it in like a normal command and it will cause the shell to wait. If you add & then the process will operate in the background e.g.

#!/bin/bash
scripting stuff here
/usr/local/bin/ymessenger &
/usr/bin/mozilla
cleanup stuff here
exit 0

ymessenger will execute in the background and the script will continue. Mozilla will execute and the script will wait for it to terminate before the cleanup starts.


Last edited by /bin/bash; 08-11-2003 at 05:14 AM.
 
  


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
bash/ksh variables question tpe Programming 2 08-11-2005 10:39 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 02:00 PM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 10:57 AM
variables in bash haddad Linux - General 6 09-22-2004 06:29 PM
Variables in bash tcaptain Programming 1 03-03-2003 03:07 PM


All times are GMT -5. The time now is 06:45 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration