0

I need to implement the following psudo code using bash.

tmp_1=""
tmp_2=""
for var in "arg_list"
do
   if[var is eq to "str_1"]
   do
      var++ # shift the loop iter to next argument
      tmp_1 = var # assign the next string/argument to 'tmp_1' variable
   done
done

eg: if i run my bash script as follow, then the value of tmp_1 should be 'str_2'

<my_script>  str_1  str_2

I try to do this with 'shift' in bash, but it didn't work to me. how to do this implementation?

1 Answer 1

5

Don't use a for loop. Just process $1 every time through the loop, and use shift to adjust the argument list.

while [ $# -gt 0 ]
do
    if [ "$1" = "str_1" ]
    then
        shift
        tmp_1="$1"
    fi
    shift
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.