-2

I am new to scripting.

My code is

#!/bin/bash
path=$1
if [ condition ]; then
.......
fi

How do i check if the argument is passed while calling the script?(What should i write as condition inside if statement)

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
Nithin Jose
  • 1,029
  • 4
  • 16
  • 31
  • 2
    `()` is not part of the bash `if` syntax for the record. Have you tried looking at any bash scripting tutorials? This should be fairly easy to find in just about all of them. – Etan Reisner Feb 06 '15 at 15:37
  • 1
    sorry that was a mistake, c programming hangover – Nithin Jose Feb 06 '15 at 15:39
  • Look at http://stackoverflow.com/questions/4423306/how-do-i-find-the-number-of-arguments-passed-to-a-bash-script – Thiyagu Feb 06 '15 at 15:39
  • Have a look at: [Introduction to if](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html) – user3439894 Feb 06 '15 at 16:02

2 Answers2

0

The syntax of a bash if statement is:

if statement; ... then statement; ... else statement; ... fi

The statements following then are executed if the last statement before then succeeded; otherwise the statements following else (if it is present; else is optional.)

The number of arguments passed to a function is $#.

You can do arithmetic comparisons with a conditional statement, whose syntax is (( arithmetic expression )). (The bash manual doesn't have a precise index tag for (( )); it's just underneath the provided link, which is for the select statement.)

rici
  • 234,347
  • 28
  • 237
  • 341
0

And finally found the answer with the help of all

if(($#==1));#check if number of arguments is 1 and return a boolean value
then
<code>
fi
Nithin Jose
  • 1,029
  • 4
  • 16
  • 31