Register
It is currently Wed Oct 22, 2014 5:07 pm

Re-assigning $2


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu May 10, 2012 5:40 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Can the build in variable $2, for example, be re-assigned if it is not supplied on the command line?

Example:

Code:
if test ! $2; then
   echo No second argument entered. Defaulting to XYZ.
   $2=XYZ
fi



Obviously that does not work, but it shows the idea of supplying a default value for $2.

I've messed around a bit with the argv syntax trying to see what it might do.

Ideas?

And as always many thanks.


Top
 Profile  
 PostPosted: Thu May 10, 2012 4:19 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi geelsu!

The way I've worked around this in the past is by simply assigning a variable name to whatever you want "$2" to ultimately be. That way you can use a simple 'if' statement like the one you have to create a default value if one is not provided.

Something like:
Code:
if [ -n $2 ]; then
   fruit=$2
else
   fruit=bananas
fi


Or here's a slightly more compact version:
Code:
fruit=$([[ -n "$2" ]] && echo "$2" || echo "bananas")


I'm sure there's a cleaner way to do it, but this has always worked for me!


Top
 Profile YIM  
 PostPosted: Thu May 10, 2012 10:21 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

Code:
[ -z "$2" ] && set -- "$1" "my second parameter"

jeo: more compact
Code:
fruit="${2:-bananas}"
;)


Top
 Profile  
 PostPosted: Fri May 11, 2012 4:43 am   

Joined: Wed May 19, 2010 9:56 am
Posts: 21
Thanks jeo and Watael.

I went ahead with Watael's first code snippet i.e. the -z version. It is the one where I don't have to say fruit=something.

The result I was looking for was an echo $1 $2 that gave me the default value or the supplied value and not echo $1 $fruit

So I now have:
Code:
if test ! $2; then
   [ -z "$2" ] && set -- "$1" "banana"
fi


Watael: Was that your intention

Now whenever I echo $1 and $2 I see either banana or the value I entered the command line. This saves me a lot of time redoing a script that uses $2 in multiple places.


Top
 Profile  
 PostPosted: Fri May 11, 2012 5:31 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
test ! "$2"
and
[ -z "$2" ]
mean the same thing: [ is an alias for test.

don't forget to quote variables!


Top
 Profile  
 PostPosted: Mon May 14, 2012 8:00 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Beautiful, thanks Watael :)


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP