LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-10-2008, 01:37 PM   #1
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Command line arguments -- what form are they?


Im trying to take a command line argument that runs a shell script to use the argument as a command line argument for some perl code I am writing.

example:

./bash 5
start=$1;
./perl start;
my $number=$ARGV[0];

In essence, perl is not seeing the argument as a number -- atleast I think thats whats going on.
 
Old 09-10-2008, 01:48 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
if your trying to pass the contents of the variable 'start' to perl, you have to dereference start: '$start'
 
Old 09-10-2008, 02:47 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
And presumably it could just be:
./perl $1

To be clear, the above would be inside a bash script which is called using "scriptname 5". You don't want to call the actual bash shell and pass "5" to it....my hunch is that bash would not know what to do....
 
Old 09-10-2008, 08:03 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Also, no need to end every line with ';' in bash. Its only needed if you put multiple cmds on one line eg

if [[ $? -eq 0 ]]; then ....

instead of

if [[ $? -eq 0 ]]
then
...
 
Old 09-11-2008, 05:55 AM   #5
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
It worked with the line

./perl $1

but I believe I had a bug in my perl code. Nonetheless, does bash interpret a command line argument as a string or does it dynamically determine what it should be by what you do with it

say my command line argument was "happy" and "754", would bash know the first is a string and the second is a integer?
 
Old 09-11-2008, 08:54 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
something like hexdump is handy for showing how things are stored. In Bash, numbers are stored as strings...the interpretation depends on what commands are used.

try this:
a=345
b=456
c=$((a+b))
echo $c
c=$a$b
echo $c
 
Old 09-11-2008, 09:09 AM   #7
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by bioinformatics_guy View Post
say my command line argument was "happy" and "754", would bash know the first is a string and the second is a integer?
Perl will evaluate your variable's depending on what context you use them in.

Code:
$var1 = 3;
$var2 = 4;

print $var1 + $var2;
The above will print '7' because its a numerical context

Code:
$var1 = 3;
$var2 = 4;

print "$var1 + $var2";
The above will print "3 + 7" since there are quotes and you use it in a string context.
 
Old 09-12-2008, 09:04 AM   #8
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Brilliant! I had a look over the code you sent and believe I understand -- the context of what you do to the variable will define how it is classified by bash/perl, but for all intensive purposes, its originally stored as a string.
 
Old 09-12-2008, 09:16 AM   #9
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Quote:
Originally Posted by bioinformatics_guy View Post
Brilliant! I had a look over the code you sent and believe I understand -- the context of what you do to the variable will define how it is classified by bash/perl, but for all intensive purposes, its originally stored as a string.
Yes but be careful, perl is a bit smarter and resourceful that bash. Perl has mathematical operations built into it, but bash will actually treat everything as a string by itself. You must use extra commands to get mathematical operations ('expr' and 'bc' come to mind). Those commands will interpret the variables as numbers and will fail if they are not.
 
  


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
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 10:40 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 09:43 AM
shell script works form command line but not form crontab saifee General 1 10-14-2004 11:27 AM
how do I copy a whoel folder form one directory to another form the command line? zwyrbla Linux - Newbie 8 08-24-2004 07:40 PM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 03:25 AM


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