LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 12-08-2003, 12:46 PM   #1
cortez
LQ Newbie
 
Registered: Dec 2003
Distribution: Debian Sid
Posts: 5

Rep: Reputation: 0
bash script $n variable expansion


Hello!

I have a problem with this (very simple indeed) script:

if [ -z "$1" ]; then
echo -e "\npsfind v0.3"
echo -e "\tUsage: psfind process1 process2 ...\n"
else
for (( i=0; i<$#; i++ )) do
ps aux | grep -v psfind | grep ${$i} | grep -v grep
done
fi

in the third grep I want the variable $0, $1, etc to be substituted with the argument passed to the script. this way it doesn't work, and ${${i}} doesn't work either.

Any ideas?

thanks



Luca
 
Old 12-08-2003, 01:06 PM   #2
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
You would use:
Code:
"$0" "$1" "$2" ---"${10}" "${11}"
Tarts
 
Old 12-08-2003, 01:14 PM   #3
cortez
LQ Newbie
 
Registered: Dec 2003
Distribution: Debian Sid
Posts: 5

Original Poster
Rep: Reputation: 0
Maybe I couldn't explain right what I needed help in, I don't get your point.

The code I showed is not working. I'd like to know how to:

If I do

./myscript foo bar

i'd like

${${i}}

the first time to expand to $1 and then to foo

the second time to expand to $2 and then to bar


thanks

Luca
 
Old 12-08-2003, 01:31 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
I'm not able to do the thing you want, but I am able to show you another way (roughly same as your original) that will do what you want:
Code:
#!/bin/bash

ALLINPUT="$@"

if [ ! -z "${ALLINPUT}" ]
then
  for PROCESS in ${ALLINPUT}
  do
    ps -aux | egrep -v 'psfind|grep' | grep ${PROCESS}
  done
else
  echo -e "\npsfind v0.3"
  echo -e "\tUsage: psfind process1 process2 ...\n"
fi
$@ holds all that was given to the script.
The for PROCESS in ${ALLINPUT} will keep working untill all in ${ALLINPUT} is given to the do ..... done loop. So you don't need any counters to check id there are any left.

I also turned around the error part. Why?? this way you have a little speed increase. Most of the times you will give an argument to psfind, so if you check that first you win a few clock sicles
 
Old 12-08-2003, 01:55 PM   #5
cortez
LQ Newbie
 
Registered: Dec 2003
Distribution: Debian Sid
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot...

That's quite a nice solution, but my mind just asks for the solution of that variable expansion thing... i just need to know how I do that!

just for the form you can also change

if [ ! -z $1 ]

in

if [ -n $1 ]


bye

Luca
 
Old 12-08-2003, 02:27 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Ok, let's do it your way
Code:
#!/bin/bash

if [ -n "$1" ]
then
  for (( i=1; i<=$#; i++ ))
  do
    ps aux | egrep -v 'psfind|grep' | eval grep $`echo $i`
  done
else
  echo -e "\npsfind v0.3"
  echo -e "\tUsage: psfind process1 process2 ...\n"
fi
I did change the 'i=0; i<....' to 'i=1; i<=......'. Otherwise you will start with the name of the program itself ($0) before the parameters you suplied.
 
Old 12-08-2003, 05:03 PM   #7
cortez
LQ Newbie
 
Registered: Dec 2003
Distribution: Debian Sid
Posts: 5

Original Poster
Rep: Reputation: 0
Great!

Thanks! Didn't knoe about eval.

I'm a



bye

Luca
 
  


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
How to export environment variable from a bash script dimaash Linux - Newbie 20 08-09-2011 08:12 AM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 08:07 AM
Bash Script Expansion Problem meadensi Linux - Newbie 1 03-04-2005 06:17 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 10:22 AM
Bash script renaming a variable zael Programming 3 09-30-2003 05:37 AM


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