2

Looping in the current directory and checking if the file extension ends with yaml, then split the filename on dot and perform other operations. Below is my code.

What's wrong here? I'm getting error like: syntax error: unexpected "(" (expecting "done")

for fname in $(find . -name '*.yaml' -exec basename {} \;); do
  echo "Printing filename $fname"
  ARR=(${fname//./ })
  echo "${ARR[0]}"
done
zubug55
  • 729
  • 7
  • 27
  • 1
    You are using a shell that cannot handle arrays. – Cyrus Nov 22 '20 at 05:46
  • what can I do to achieve the result as above? – zubug55 Nov 22 '20 at 06:03
  • Show how to start the script. – Cyrus Nov 22 '20 at 06:05
  • this script is getting executed in a Tekton pipeline. under`script: |-` – zubug55 Nov 22 '20 at 06:09
  • I assume that the script is started with `sh` instead of `bash`. This might help: `bash script.sh` – Cyrus Nov 22 '20 at 06:12
  • No, I have directly return the code above under `script: |-` without any header as sh or bash. – zubug55 Nov 22 '20 at 06:15
  • @Cyrus by default it is getting started with `#!/bin/sh` – zubug55 Nov 22 '20 at 06:19
  • So, any recommendation on how do I get its work now? – zubug55 Nov 22 '20 at 06:20
  • Use `basename "$fname" .yaml` to print the base name of the file without the file extension. No need for the array at all, nor is there a need to echo the result because `basename` writes to standard output anyway. Unless, perhaps, you might have a name like `part1.part2.yaml`, in which case your script would echo `part1` and `basename "$fname" .yaml` would echo `part1.part2`. – Jonathan Leffler Nov 22 '20 at 06:23
  • I have a name like part1.part2.part3.yaml and that's why I have to break this name on dot and then use part1, part2, part3 to execute different parts of the script. – zubug55 Nov 22 '20 at 06:28
  • Can you post and answer with description. That will be fantastic. – zubug55 Nov 22 '20 at 06:28
  • The array manipulation here is just crazy. Assuming you don't have file names with newlines in them, `find . -name '*.yaml' | sed 's%.*/\([^/.]*\)\.[^/]*$%\1%'` – tripleee Nov 25 '20 at 07:31

1 Answers1

-1

This script may be able to meet your needs:

for fname in *.yaml; do
  echo "Printing filename $fname"
  ARR[i]=`echo $fname | sed 's/\..*//'`
  echo "${ARR[i]}"
  i=$i+1;
done
MD128
  • 501
  • 4
  • 12