I am trying to write a simple script with the following logic: IF the first argument is a valid regular file, run the for-loop; if no argument is provided, run "code2"; all else, run "code3". The following script seems Okay to me, but it got stuck if run without an argument.
#!/bin/bash
if [ -f $1 ]; then
for i in `cat $1`
do
<something>
done
elif [ $# -eq 0 ]; then
"code2"
else
"code3"
fi
Here is the debug output and it got stuck at the end.
bash -x myscript
+ '[' -f ']'
++ cat
Since I provide no argument, why does it still run into the first if-then flow? I am new to bash scripting. Any help would be thankful.