0

I am trying to use multiple inputs at the same files in a command as an output, from this question I made

looping object as inputs in BASH, SHELL script

The user Konsolebox and all the others were very helpful in giving me to understand how the process may be improved and operate. After their suggestion I took their inputs and tried to alter my code, although it does not seem to work.

The code's job is to take multiple items with difference in time and/or months and input them as input files to a bash command which will produce one single output file

The suggestions proposed made me alter the code this way:

fname2="trim"
years=({2000..2010})
months=(01 02)  ## ({01..100}) should be the same
end="nc"

I have been trying to make it work but no matter what i change i get the same error

cdo mergetime: Open failed on >@({2010})@(01<
No such file or directory

I have placed the bash script in the working folder and the files are named trim201001.nc and trim201002.nc (usually i will have hundreds with that format that is why i am trying to automatize them)

I am running the script with the command ./script.sh

after the advice of konsolebox i added shopt -s nullglob

shopt -s extglob
shopt -s nullglob
#IFS='|' eval 'pattern="${fname2}@(${years[*]})@(${months[*]}).${end}"'
IFS='|' eval 'pattern="${fname2}$years@(${months[*]}).${end}"'

echo "pattern = ${pattern}"
files=($pattern)  ## pathname expansion is sorted in Bash.

cdo mergetime "${files[@]}" ofile

And the new error i think is located in the month reading, although i checked my location the one file that is to be use is 02 (month) and 01 (month)

pattern = @(2010)@(01|02).nc

cdo mergetime: Open failed on >02).nc<
No such file or directory
Community
  • 1
  • 1
George
  • 128
  • 3
  • 12
  • What does `echo "$pattern"` say if you insert it after the second command? And it should have been a good thing that you have added `shopt -s nullglob` (I should have suggested that sorry). – konsolebox Jul 09 '14 at 15:02
  • And make sure you run your script with bash e.g. `bash script.sh` not `sh script.sh` – konsolebox Jul 09 '14 at 15:05
  • I also noticed: `years=({2000...2010})` has 3 dots. It should only be two: `years=({2000..2010})` – konsolebox Jul 09 '14 at 15:07
  • @konsolebox Whereshould i add the shopt -s nullglob? i changed the years = ({2010}), although it still but the error is the same. – George Jul 09 '14 at 15:42
  • You can add it after `shopt -s extglob`. That should be valid with bash. Are you running your script with `bash your_script.sh`? What version of bash do you have? See `bash --version`. – konsolebox Jul 09 '14 at 15:43
  • Adding `nullglob` means that no value would be given even if no match is found. By default a pattern yields itself if a file is not found on it. e.g. `echo *` would print `*` if no file exists. Probably you can also test your command with `[[ ${#files[@]} -gt 0 ]] && cdo mergetime ...` which would only execute the command if files are found. – konsolebox Jul 09 '14 at 15:45
  • @konsolebox I am using GNU bash 4.1.2, and i run my file in this manner ./script.sh – George Jul 09 '14 at 15:54
  • Does it make any difference if you run it with `bash script.sh`? Is the header set as `#!/bin/bash` and not `#!/bin/sh`? – konsolebox Jul 09 '14 at 15:58
  • @konsolebox #!/bin/sh i have other scripts with that and it works – George Jul 09 '14 at 16:11
  • Well perhaps the other scripts don't need functions that are specific to Bash. Many of the functions on the example solution I proposed were bash-specific. – konsolebox Jul 09 '14 at 16:37
  • @konsolebox I use bash just as a black box to be honest, the scripts i am writing often times are called to execute other toolboxes. I added the command you told me and the error now has been limited to the month, please see the question above i have inserted the new error. – George Jul 10 '14 at 08:13
  • Having only one argument in `{}` is no longer a form of brace expansion. You need to have two i.e. `{A..B}`. See [Brace Expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html). If you only want to give one element to your array, just do `years=(2010)`. And I actually wonder why you don't get the proper pattern for months. It should be `@(01|02)`. – konsolebox Jul 10 '14 at 09:47
  • I'm sorry. The declaration to `IFS` has a typo. It should be `IFS='|'` not `ISF='|'`. – konsolebox Jul 10 '14 at 09:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57081/discussion-between-george-and-konsolebox). – George Jul 10 '14 at 10:06

0 Answers0