0

I tried a lot of things, but this design does not want to work in any way

#!/bin/bash

trap 'echo "# $BASH_COMMAND";read' DEBUG


test () {  
    case "$2" in
    3) echo "3" 
       ;;
    4) echo "4" 
       ;;
    esac
}

while :
do
case "$1" in 

    1) echo "1" ;;
    2) test;;

esac
done

if i use case in case it/s work, and when i use read to $2 it work, but i want to work with keys from command line

4
  • 3
    You’re calling test (not a good function name, BTW) without any arguments. $2 within test is not set. Commented Nov 18, 2022 at 10:30
  • 2
    Or to state it explicitly: Outside of a function $1, $2, ... will be the arguments your script was called with (unless re-set using set), but inside of a function those will have the values of the arguments to that function and you called test without any arguments. Commented Nov 18, 2022 at 10:35
  • tnx and sorry for my simpl mistake, now everything works as it should Commented Nov 18, 2022 at 10:50
  • This doc might be helpful Commented Nov 18, 2022 at 11:04

1 Answer 1

1
test () {  
    case "$1" in

    3) echo "3"
       exit 0 
       ;;
    4) echo "4"
       exit 0 
       ;;
    esac
}

while :
do
case "$1" in 

    1) echo "1" ;;
    2) test $2;;

esac
done
Sign up to request clarification or add additional context in comments.

2 Comments

But of course it is a terrible idea to name a function like a standard utility. Even if the OP did it - we shouldn't repeat poor design choices in an answer. I suggest that you change test into, i.e., test4711.
i used "test" just for test ))

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.