In this article, we will see how to user IF statements and conditional expressions to check if a file exist or folder exist to perform execution depend on result in bash shell script in Linux with adding a check to see how many parameter has been passed to script as argument.
To recall parameters, "$1" was checking first argument, and following each n+1 with "$" sign at beginning returns corresponding parameter within script. To check total parameter count which has been passed to script, we can use "$#" variable.
Lets look to its syntax.
if [ $# -eq 2 ]; then
echo "2 parameter passed as argument"
else
echo "less then or more than 2"
fi
Lets see it in bash shell scripting example.
As above output indicates, we added a basic IF statement to a shell bash script with name "1.sh". Our script takes 2 parameter from shell call. First parameter is folder name, second parameter is file name. We assign those to parameters in our script, and perform if given parameters exist as file name and folder name.
Our script at beginning checks how many parameter has been passed to it in shell call. We had passed 2 parameter, 1st for filename, 2nd for filename. It returned 2 as expected.