0

How can I do something like this

   if [[ $variable = name ]]
  then
      $variable=fname
fi 
if [[ $variable = surname]]
  then
      $variable=sname
 fi    

so change the value based on on result as I have another web based script that sets variable to a user friendly name, but I need the script to change it to the correct one. users will likely select multiple so i need it to change based on their choice

6
  • I'm not entirely sure I understand what you want to do. What values should the variable be set to, and based on what? What does "web based script" mean and does it have any bearing on your question?
    – Kusalananda
    May 6 '20 at 11:39
  • basically the variable will be defined as one thing above and i need to change it before the script uses it
    – Andy
    May 6 '20 at 11:42
  • 1
    @Andy so what you mean is that the name of the variable changes? This... is not a very good idea. There are much cleaner and better ways of approaching this such as using arrays for example. Can you give us some more details about what your script is doing and what you really need to achieve? (see XY problem).
    – terdon
    May 6 '20 at 11:52
  • 1
    Please edit your question and explain this. Comments are easy to miss, hard to read and can be deleted without warning. Also explain i) why you don't just use the same variable names? ii) why the name of the variable is important instead of its value (why don't you do something like if [[ $variable1 = "food" ]]; then variable2="groceries" or whatever).
    – terdon
    May 6 '20 at 12:07
  • 1
    Is it correct to say that, in your program, 1) your variable is initially assigned a value from a known set; 2) you also have a second set of values, which maps onto the first set; 3) you want to assign variable a value from the second set based on its current value? E.g. case "$variable" in curval) variable=newval;; othercurval) variable=othernewval;; esac?
    – fra-san
    May 6 '20 at 12:43
3

Check your assignment syntax, you should not have a $ there. $ is for expanding variables.

# standard sh syntax
case $variable in
  (name)    variable=fname;;
  (surname) variable=sname;;
  (*)       printf >&2 '%s\n' "$variable not supported"; exit 1;;
esac

See also:

# ksh93/bash/zsh specific
typeset -A map=(
     [name]=fname
  [surname]=sname
)

variable=${map[$variable]?$variable not supported}

(beware that bash's associative arrays don't support the empty string as a key value).

In any case, you don't want to write a web CGI that takes user input as a shell script, as you'd be almost certain to introduced security vulnerabilities, especially if you're not a shell script expert.

2
  • To Further explain The variable can come in as multiple thins, Example $groceries=food or groceries=food,wine or even groceries=food,beer So multiple things How can i change all those depending how they come in
    – Andy
    May 7 '20 at 9:57
  • @Andy, that sounds like a different question from the one currently stated. So you may want to ask a separate question. It also sounds you'd be better of using a proper programming language instead of a shell here, even if it's just awk even if the code for that programming language is embedded in a shell script. May 7 '20 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.