my code is something like this
for i in $1 $2 $3 $4; do
if [[("$i" == '-f')]] ; then #I'm searching if the user input the -f switch
if [ -f "$((i+1))" ]; then # I'm trying to increment the position of $i to get the input that follows the -f switch and check the existence of the file
echo "$((i+1)) file found"
echo "$((i+!)) file not found"
You should almost certainly use
getopts, but you could just set a flag while iterating:
#!/bin/bash unset FLAG for x; do if test -n "$FLAG"; then FILENAME=$x unset FLAG continue fi case $x in -f) FLAG=1;; # Set flag for next iteration *) echo "processing argument $x" esac done echo "FILENAME=$FILENAME"
This is terribly fragile, and I feel somewhat guilty suggesting it. Really, use getopts instead of trying to roll your own argument parsing.