LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command line arguments -- what form are they? (https://www.linuxquestions.org/questions/linux-newbie-8/command-line-arguments-what-form-are-they-668973/)

bioinformatics_guy 09-10-2008 12:37 PM

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.

CRC123 09-10-2008 12:48 PM

if your trying to pass the contents of the variable 'start' to perl, you have to dereference start: '$start'

pixellany 09-10-2008 01:47 PM

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....

chrism01 09-10-2008 07:03 PM

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
...

bioinformatics_guy 09-11-2008 04:55 AM

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?

pixellany 09-11-2008 07:54 AM

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

CRC123 09-11-2008 08:09 AM

Quote:

Originally Posted by bioinformatics_guy (Post 3276885)
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.

bioinformatics_guy 09-12-2008 08:04 AM

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.

CRC123 09-12-2008 08:16 AM

Quote:

Originally Posted by bioinformatics_guy (Post 3278259)
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.


All times are GMT -5. The time now is 01:00 AM.