last day (18 days later) » 

10:06 AM
0
Q: Inserting many objects in Bash script

GeorgeI 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 s...

 
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).
And make sure you run your script with bash e.g. bash script.sh not sh script.sh
I also noticed: years=({2000...2010}) has 3 dots. It should only be two: years=({2000..2010})
 
@konsolebox Whereshould i add the shopt -s nullglob? i changed the years = ({2010}), although it still but the error is the same.
 
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.
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 I am using GNU bash 4.1.2, and i run my file in this manner ./script.sh
 
Does it make any difference if you run it with bash script.sh? Is the header set as #!/bin/bash and not #!/bin/sh?
 
10:06 AM
@konsolebox #!/bin/sh i have other scripts with that and it works
 
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 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.
 
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. 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).
I'm sorry. The declaration to IFS has a typo. It should be IFS='|' not ISF='|'.
 
hello
ok i thought it would not matter that the {} include only one value, so now i have changed the code to the current infomation including the IFS
but still the same error, i shall refresh the code in the question box to be clear
 
 
2 hours later…
12:16 PM
Can you show me the output of:
#!/bin/bash

fname2="trim"
years=(2010)
months=(01 02)
end="nc"

shopt -s extglob
shopt -s nullglob
IFS='|' eval 'pattern="${fname2}@(${years[*]})@(${months[*]}).${end}"'
echo "pattern: ${pattern}"
files=($pattern)
echo "files: ${files[*]}"
 
 
1 hour later…
1:20 PM
i pasted that solution
and the message is
pattern: trim@(2010)@(01|02).nc
files: 02).nc
 

  last day (18 days later) »