-1

I am using the getopts built-in function to parse arguments to my bash script. I then take the values assigned to each flag and use in a subsequent command.

Part of myscript.sh:

while getopts "s:n:e" opt; do
 case $opt in
n) name=$OPTARG;;
e) area=$OPTARG;;
s) part+=("$OPTARG");;
 esac
done
shift $((OPTIND -1))

getArray(){ for val in "${part[@]}"; do
  echo "$val"
done }

getPart(){ getArray | while read -r param1; do 
 echo "$param1" | sed 's/=.*//'}
done }
parts=$(getPart)

I have a info.csv config file:

name,part,area,partcode
fiat,exhaust,store,123
fiat,engine,store,132
ford,exhaust,store,145
ford,windscreen,store,134

Once the command is run I use grep "$name,$part,$area" info.csv | cut -d, f4 to extract a specific field based on what the user inputs as the values of their flags when the script is run.

I use the -s flag either once, twice or three times with different values each time I run the script.

The values come in the wrong format (i.e. -s fiat=1 -s exhaust=2 and the column I am using them to access in the grep statement above requires them to be in this format fiat and exhaust ) so I use sed to remove the =1 and =2.

I want to reuse these now correctly formatted values as my $part variable. But I am having trouble reassigning the newly formatted values back to the $part variable.

My question would be:

  1. How do you reassign variables in bash after performing a command on them to get them in the correct format?
bashprofile84
  • 69
  • 1
  • 8
  • It would really help if you could edit your question and add a subset of your bash code in order to get a minimal working example. One line of code is always clearer than ten words explaining what the code is doing. – Pierre François Mar 25 '20 at 09:22
  • @PierreFrançois I thought that I'd done that by adding the ```grep``` command. If I were to run ```./myscript.sh -n fiat -s fiat=1 -s exhaust=1 -e store``` the ```grep``` command isn't able to handle the two values assigned to the ```-s``` flags – bashprofile84 Mar 25 '20 at 09:31
  • Posting more lines of `myscript.sh` than the 3 lines you posted above would help. – Pierre François Mar 25 '20 at 09:43
  • if a string variable (lets say `$var`) must be stripped from its end, everything after the `=` in your case, you don't need `sed`, you just have to assign `var=${var%%=*}`. – Pierre François Mar 25 '20 at 09:53
  • @PierreFrançois I have updated the question with more lines of code.. so you see I store the ```-s``` flags in an array and then perform the stripping on the array and then assign those values to ```$parts``` but I have been trying to update the ```grep``` command so that ```$part``` contains the values in ```$parts``` so it has the ability to extract multiple ```partcode``` if the ```-s``` flag is used more than once i.e. multiple ```part``` values are parsed as arguments for the ```$part``` variable – bashprofile84 Mar 25 '20 at 10:28
  • What is the `$comp` variable and what do you want to do with it? – Pierre François Mar 25 '20 at 10:34
  • @PierreFrançois The ```$comp``` variable was a typo.. should have been ```$part``` variable.. I have updated the question. – bashprofile84 Mar 25 '20 at 10:41

1 Answers1

2

If I understand you correctly, you assign a value to the array ${part[@]} and after that, you want to remove the end of the string from the = sign on for each element in the array.

Instead of looping over the array in a second pass, I would just initialize each element of the array correctly from the very beginning in the following way:

...
s) part+=("${OPTARG%%=*}");;
...

See bash manual for more explanation about removing matching suffix pattern.

Pierre François
  • 5,850
  • 1
  • 17
  • 38
  • Yes, you have understood correctly! Thank you for that. I now run this with the ```s)``` updated and it extracts the ```partcode``` only for this ```-s fiat=1``` and not for the 2nd flag value i.e. ```-s exhaust=2``` – bashprofile84 Mar 25 '20 at 11:13
  • If this solution works, please accept my solution by clicking on the grey ✓ near my answer. Feel free also to upvote my solution with the ▲ button above the check mark. – Pierre François Mar 25 '20 at 11:45
  • Are you able to point me to a link where I can find out how to use a while loop to ```read``` the contents of an array and then ```do``` the ```grep``` command in this loop to allow the ```partcode``` to be extracted for not just one ```-s``` but multiple? – bashprofile84 Mar 25 '20 at 11:54
  • For looping over all the elements of an array, you can issue: `for e in "${part[@]}"; do echo working on "$e"; done`. I am afraid I don't understand exactly the second part of your question. – Pierre François Mar 25 '20 at 14:04
  • If you want an answer to a new question, you better post a new a question, because very few people will notice the question you are asking in the above comment. – Pierre François Mar 25 '20 at 14:07
  • Please see my new question here: https://stackoverflow.com/questions/60849788/how-to-run-a-command-multiple-times-for-each-line-of-a-variable?noredirect=1#comment107658534_60849788 – bashprofile84 Mar 25 '20 at 14:08