2

In my bash function, I am using grep to capture a matching pattern from a string (selecting the files created), and then storing what is captured in an array. The string is assigned to a variable called output

installing route
  create app/routes/foo.js
  create app/templates/foo.hbs
updating router
  add route foo
installing route-test
  create tests/unit/routes/foo-test.js

after running

 files=($(echo "$output" | ggrep -oP 'create\s\K(.+)')) 

I confirm that I am capturing what I intend to by running echo ${files[*]}. Output in the terminal looks as so

app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js

My goal is to pass these files as arguments to an npm script (npm run lint <list of files>). However, when I try to plug in my variable to the npm script execution, the file names either print out like

"app/routes/foo.js" "app/templates/foo.hbs" "tests/unit/routes/foo-test.js"

or

"app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js"

The ultimate goal here is to be able to run

npm run lint app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js

I have tried tons of combinations to interpolate my file variable, but nothing seems to work. I feel like I am missing something conceptually or approaching this in the wrong way. I originally was trying this without using an array for file, but I was running into the same issue. My first pass is getting this to run in bash. However, I'd prefer to stick with using grep or sed here though. Anyone have any suggestions?

After reading suggestions I have tried

parsedfiles=$(echo "${files[*]}" | sed  -e 's/"//g')
echo $parsedfiles
npm run lint $parsedfiles

When echo $parsedfiles runs my terminal output looks good app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js, but on the next line, it still outputs as npm run "app/routes/foo.js" "app/templates/foo.hbs" "tests/unit/routes/foo-test.js"

SOLVED The real issue here wasn't about how I was passing the args in. It's an issue with actually running the npm script. When passing args to the npm script it automatically puts them in double quotes.

Scott Davidson
  • 995
  • 1
  • 9
  • 12
  • What is the error that occurs currently when the file paths are passed to npm as individual or one large string? – MicFin Aug 20 '17 at 03:07
  • Have you tried these approaches? https://stackoverflow.com/questions/9590034/getting-bash-variable-into-filename-for-zip-command or https://stackoverflow.com/questions/16703624/bash-interpret-string-variable-as-file-name-path – MicFin Aug 20 '17 at 03:11
  • This might be a good use case for eval? https://stackoverflow.com/questions/11065077/eval-command-in-bash-and-its-typical-uses – MicFin Aug 20 '17 at 03:19
  • Thank you for the links! The issue ended up not being with the bash script, but rather passing arguments to the npm run command that was wrapping the filenames in double quotes. – Scott Davidson Aug 20 '17 at 15:27

1 Answers1

1

Use sed to edit the text as below.

$ var=`echo "app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js" | sed  -e 's/"//g'`

test$ echo $var
app/routes/foo.js app/templates/foo.hbs tests/unit/routes/foo-test.js

Now you can use var in npm run lint

$ npm run lint $var
nagendra547
  • 5,672
  • 3
  • 29
  • 43
  • It appears to look good when I echo $var, but when I interpolate the variable into the `npm run lint command` files still look like `"app/routes/foo.js" "app/templates/foo.hbs" "tests/unit/routes/foo-test.js"` – Scott Davidson Aug 20 '17 at 14:39
  • Ended up being an issue with running the npm script with arguments, as this is what was putting it in quotes. To accomplish what I originally described this is a great solution. Thanks for you help! – Scott Davidson Aug 20 '17 at 14:52