Skip to main content

I have this code in a tool I am currently building: currently building:

while [ $# -gt 0 ]; do
  case "$1" in
    --var1=*)
      var1="${1#*=}"
      ;;
    --var2=*)
      var1="${1#*=}"
      ;;
    --var3=*)
      var1="${1#*=}"
      ;;
    *)
      printf "***************************\n
              * Error: Invalid argument.*\n
              ***************************\n"
  esac
  shift
done

I have many options to add, but five of my options should be saved as arrays. So if I call the tool, let's say from the shell using something like this:
./tool --var1="2" --var1="3" --var1="4" --var1="5" --var2="6" --var3="7"

How can I save the value of var1 as an array? Is that possible? And, if so, what is the best way to deal with these arrays in terms of efficiency if I have too many of them?.

I have this code in a tool I am currently building: currently building:

while [ $# -gt 0 ]; do
  case "$1" in
    --var1=*)
      var1="${1#*=}"
      ;;
    --var2=*)
      var1="${1#*=}"
      ;;
    --var3=*)
      var1="${1#*=}"
      ;;
    *)
      printf "***************************\n
              * Error: Invalid argument.*\n
              ***************************\n"
  esac
  shift
done

I have many options to add, but five of my options should be saved as arrays. So if I call the tool, let's say from the shell using something like this:
./tool --var1="2" --var1="3" --var1="4" --var1="5" --var2="6" --var3="7"

How can I save the value of var1 as an array? Is that possible? And, if so, what is the best way to deal with these arrays in terms of efficiency if I have too many of them?.

I have this code in a tool I am currently building:

while [ $# -gt 0 ]; do
  case "$1" in
    --var1=*)
      var1="${1#*=}"
      ;;
    --var2=*)
      var1="${1#*=}"
      ;;
    --var3=*)
      var1="${1#*=}"
      ;;
    *)
      printf "***************************\n
              * Error: Invalid argument.*\n
              ***************************\n"
  esac
  shift
done

I have many options to add, but five of my options should be saved as arrays. So if I call the tool, let's say from the shell using something like this:
./tool --var1="2" --var1="3" --var1="4" --var1="5" --var2="6" --var3="7"

How can I save the value of var1 as an array? Is that possible? And, if so, what is the best way to deal with these arrays in terms of efficiency if I have too many of them?.

Improved English and removed salutations, they are implied here.
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

I have this code on myin a tool, that I am currently building: currently building:

while [ $# -gt 0 ]; do
  case "$1""$1" in
    --var1=*)
      var1="$var1="${1#*=}""
      ;;
    --var2=*)
      var1="$var1="${1#*=}""
      ;;
    --var3=*)
      var1="$var1="${1#*=}""
      ;;
    *)
      printf "***************************\n"***************************\n
              * Error: Invalid argument.*\n
              ***************************\n"***************************\n"
  esac
  shift
done

So I have many options to add, but five of my options should be put inside arraysaved as arrays. So if I call the tool, let's say from the shell using something like this:
./tool --var1="2" --var1="3" --var1="4" --var1="5" --var2="6" --var3="7" As you

How can see here, var1 isI save the value of var1 as an array, should be! So my first question is? Is that possible?? And, if so, what is the best way to deal with these arrayarrays in termterms of the efficiency, if I have too many of them??.

So guys I would really appreciate your help, since I am new with bash script.

Thanks!!

I have this code on my tool, that I currently building:

while [ $# -gt 0 ]; do
  case "$1" in
    --var1=*)
      var1="${1#*=}"
      ;;
    --var2=*)
      var1="${1#*=}"
      ;;
    --var3=*)
      var1="${1#*=}"
      ;;
    *)
      printf "***************************\n
              * Error: Invalid argument.*\n
              ***************************\n"
  esac
  shift
done

So I have many options to add, but five of my options should be put inside array. So if I call the tool, let's say from the shell something like this:
./tool --var1="2" --var1="3" --var1="4" --var1="5" --var2="6" --var3="7" As you can see here, var1 is an array, should be! So my first question is that possible?? And if so what is the best way to deal with these array in term of the efficiency, if I have too many of them??.

So guys I would really appreciate your help, since I am new with bash script.

Thanks!!

I have this code in a tool I am currently building: currently building:

while [ $# -gt 0 ]; do
  case "$1" in
    --var1=*)
      var1="${1#*=}"
      ;;
    --var2=*)
      var1="${1#*=}"
      ;;
    --var3=*)
      var1="${1#*=}"
      ;;
    *)
      printf "***************************\n
              * Error: Invalid argument.*\n
              ***************************\n"
  esac
  shift
done

I have many options to add, but five of my options should be saved as arrays. So if I call the tool, let's say from the shell using something like this:
./tool --var1="2" --var1="3" --var1="4" --var1="5" --var2="6" --var3="7"

How can I save the value of var1 as an array? Is that possible? And, if so, what is the best way to deal with these arrays in terms of efficiency if I have too many of them?.

Source Link

Passing named arguments as array in shell script

I have this code on my tool, that I currently building:

while [ $# -gt 0 ]; do
  case "$1" in
    --var1=*)
      var1="${1#*=}"
      ;;
    --var2=*)
      var1="${1#*=}"
      ;;
    --var3=*)
      var1="${1#*=}"
      ;;
    *)
      printf "***************************\n
              * Error: Invalid argument.*\n
              ***************************\n"
  esac
  shift
done

So I have many options to add, but five of my options should be put inside array. So if I call the tool, let's say from the shell something like this:
./tool --var1="2" --var1="3" --var1="4" --var1="5" --var2="6" --var3="7" As you can see here, var1 is an array, should be! So my first question is that possible?? And if so what is the best way to deal with these array in term of the efficiency, if I have too many of them??.

So guys I would really appreciate your help, since I am new with bash script.

Thanks!!