0

Wrote the following simple .sh script which should save output.txt to the working directory:

valuesfile="values.*"

for f in ./
do
 if [[ $f = $valuesfile ]]
 then
     yq d $f 'resources.' >./output.txt
 fi
done

When I execute the script, no file is created. all folders have rwx permissions including the script itself.

5
  • 1
    Check what the script outputs without the redirection to file. I'm not familiar with yq but the d looks strange. Usually one would expect an option to be proceeded by at least 1 - Commented Apr 12, 2021 at 10:46
  • Looks like the body of the if will never execute. Commented Apr 12, 2021 at 10:48
  • Yes. That's odd as well! Commented Apr 12, 2021 at 10:49
  • You should refresh yourself on how shell filename expansion works. Commented Apr 12, 2021 at 10:51
  • On the plus side, at least you're not making the common beginner mistake of trying to use ls here. Commented Apr 12, 2021 at 10:53

3 Answers 3

0

Your condition can never be true, and therefore, yq is never executed.

The loop body is executed exactly once, with f set to the string ./, and valuesfile is set to the string values.*. These strings are obviously not equal.

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

3 Comments

by values.* I meant to regex. Inside the working dir I want to take all the files which start with "values" and do the yq command on each one of them.
You don't do a regex match in your script. This would be done by the =~ operator, not by =. For finding files where the name starts with values, a regexp is overkill. Wildcard pattern match is sufficient. You could much a filename stored in variable filename simply by [[ $filename == values* ]]. Of course even this would not fix your problem, because of the non-sense value stored in your variable f....
please take a loon on my new thread: stackoverflow.com/questions/67089998/…
0

Just read this piece of code:

valuesfile="values.*"
...
 if [[ $f = $valuesfile ]]

I don't think that any file can be called values.*, I'm expecting something like values.1 or values.txt, but a filename containing an asterisk look weird to me.

Edit after comment

I propose the following:

valuesfile="values.*"

for f in ./$valuesfile
do
     yq d $f 'resources.' >./output.txt
done

8 Comments

by values.* I meant to regex. Inside the working dir I want to take all the files which start with "values" and do the yq command on each one of them.
Why do you even use regex? I believe something like for f in ./$valuefile should do the trick, isn't it? No need for the if-clause.
I've updated my answer (without testing, however), do you understand what I mean?
I understand, but valuesfile is not a repo so how can you do "for f in ./$valuesfile"? Sorry if I have lack of knowledge..
I want to go over all the files inside the directory: /usr/src/mydir/. Inside /mydir there are some files which their name starts with "values"... So I want to go over these files only and do the yq command.
|
0

Your glob is never expanded: unquoted parameter expansions inside [[ ... ]] do not undergo pathname expansions. Your loop is also incorrect: for f in ./ iterates exactly once, setting f to the string ./, rather than iteration over the files in the current directly. And finally, if there are more than one matching file, you'll repeatedly overwrite the previous contents of output.txt; use >> instead.

You could try

valuesfile="values.*"

for f in ./$valuesfile
do
     yq d "$f" 'resources.' >> ./output.txt
done

though this will cause problems if any of your files have whitespace, as the expansion of $valuesfile undergoes word-splitting as well as pathname expansion.

Use an array instead:

valuesfile=( values.* )

for f in "${valuesfile[@]}"
do
  yq d "$f" 'resources.' >> output.txt
done

Or, just dispense with the extra variable and put the pattern in the loop directly:

for f in values.*
do
  yq d "$f" 'resources.' >> output.txt
done

1 Comment

please take a loon on my new thread: stackoverflow.com/questions/67089998/…

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.