Accepting command line arguments in bash


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Accepting command line arguments in bash
# 8  
Old 03-15-2012
Both will work in bash, they do different things.

[ ] will work in any shell and support many different operators for comparing numbers, strings, checking the condition of files and directories, and so forth. See man test for details.

(()) are mathematical operators unique to bash and ksh. They can do entire algebraic statements using the traditional C-style + - * / % > < == etc. In a shell without them, you'd have to use the let syntax for math or the external expr utility to do math that complicated.

Code:
if (((X-3) > 5))

# 9  
Old 03-15-2012
Quote:
Originally Posted by kristinu
I am also trying to do and if statement and getting problems:
Code:
if (($opt_usage -eq 1))
then
  ...
  ...
fi

(( )) are for algerbra. [ ] are for other things. Use [ ] for -eq .
# 10  
Old 03-15-2012
I understand bash can only do integer comparisons. What does one do for floating point comparisons?

Ir seems that for integer comparison, I got to use the -eq as there is no ==

Code:
integer comparison

-eq
is equal to

if [ "$a" -eq "$b" ]

-ne
is not equal to

if [ "$a" -ne "$b" ]

-gt
is greater than

if [ "$a" -gt "$b" ]

-ge
is greater than or equal to

if [ "$a" -ge "$b" ]

-lt
is less than

if [ "$a" -lt "$b" ]

-le
is less than or equal to

if [ "$a" -le "$b" ]

<
is less than (within double parentheses)

(("$a" < "$b"))

<=
is less than or equal to (within double parentheses)

(("$a" <= "$b"))

>
is greater than (within double parentheses)

(("$a" > "$b"))

>=
is greater than or equal to (within double parentheses)

(("$a" >= "$b"))

Also there are no && and || in bash, but there is -a and -o.

Code:
[ EXPR1 -a EXPR2 ]	True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ]	True if either EXPR1 or EXPR2 is true.

In which cases do I use
Code:
if [[]]

rather than
Code:
if []

# 11  
Old 03-15-2012
Quote:
Originally Posted by kristinu
I understand bash can only do integer comparisons. What does one do for floating point comparisons?
Use something other than bash...

awk for instance is a full-fledged language that's found on any UNIX system which is capable of quite a bit. It's not shell, however.

Quote:
It seems that for integer comparison, I got to use the -eq as there is no ==
For [ ] , you use -eq for numbers or = for strings.

Quote:
Also there are no && and || in bash, but there is -a and -o.
There's no && or || in [ ] brackets in general. If you used [[ ]] brackets there would be. They're nearly the same as [ ] but have && and || and a few other things.
# 12  
Old 03-15-2012
Ok, I have used awk before when I was using tcsh. I am using if statements mainly for checking options.

Example, in tcsh I have make option npop active, whereas option sigma unactive. In which case I perform some operations.

Code:
set opt_npop = 1      
set opt_sigma = 0
if ( ($opt_npop == 1) && ($opt_sigma == 0) ) then
  ...
endif

How best should I do this in bash?
# 13  
Old 03-15-2012
Code:
# Note how there are NO spaces around the equal sign.
# In the bourne shell, extra spaces there changes the meaning.
opt_npop=1      
opt_sigma=0

if [ "$opt_npop" -eq 1 ] && [ "$opt_sigma" -eq 0 ]
then
...
fi

# 14  
Old 03-15-2012
Quote:
Originally Posted by Corona688
Code:
# Note how there are NO spaces around the equal sign.
# In the bourne shell, extra spaces there changes the meaning.
opt_npop=1      
opt_sigma=0

if [ "$opt_npop" -eq 1 ] && [ "$opt_sigma" -eq 0 ]
then
...
fi

I thought you said there are no && and || when using [].
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wanted: Help with escaping bash command line arguments

Please forgive me if this is the wrong forum. I want to execute some one liners with the groovy programming language and I'm having trouble escaping the special characters to accommodate bash. Here is one of the lines that is giving me trouble: groovy -e "(new... (1 Reply)
Discussion started by: siegfried
1 Replies

2. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

3. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

4. Shell Programming and Scripting

Accepting user input and arguments in PERL

Hi All, Can we pass arguments while calling the perl script and as well as ask user input during execution of the script? My program is as below: I am passing arg1 and arg2 as argements to test.pl ]./test.pl arg1 arg2 Inside the test.pl I have : print "Do you want a name ? (y/n) : ";... (2 Replies)
Discussion started by: jisha
2 Replies

5. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

6. Shell Programming and Scripting

Help parsing command line arguments in bash

Looking for a little help parsing some command line arguments in a bash script I am working on, this is probably fairly basic to most, but I do not have much experience with it. At the command line, when the script is run, I need to make sure the argument passed is a file, it exists in the... (3 Replies)
Discussion started by: Breakology
3 Replies

7. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

8. Shell Programming and Scripting

Accepting filename as command line param and writing to it

Hi, Is it possible to accept a filename as command line parameter and then write to that file using command redirection? i tried the below script. outputfile=`echo $1` echo "Writing to file" > 'echo $outputfile' exit $returncode but it isnt working. is there any other way to... (9 Replies)
Discussion started by: silas.john
9 Replies

9. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies

10. Shell Programming and Scripting

Bash: Reading 2 arguments from a command line

If no arguments are entered I wanna be able to read 2 arguments, i have done like this but it doesnt work: x=0 until #loop starts do if ; then echo No arguments were entered, please enter 2 arguments. read $1 $2 elif || ; then echo $#... (0 Replies)
Discussion started by: Vozx
0 Replies
Login or Register to Ask a Question