4

I've been programming on MVS for years and recently have been using Linux so my Linux skills are not that great yet but I understand development concepts well.

What I'm trying to do is to read a directory of files and take the name of all files with an extension of .trg. Then, I need to take those names and store them in separate variables so I can use them later in my script to refer to components and use them for file naming. I have gotten the file names using the find command:

param=`find *${file_name}*.trg

This produced the $param variable loaded with the values (ABC1099R ABC1099C) as their are two files in the directory named ABC1099R.trg and ABC1099C.trg I now need to assign those values to individual variables so I can use them later in my script. I have searched high and low on the web, but every example I find is using echo to just display them or they are using a do while loop and using the individual values within the loop. I need to have them assigned to their own variables so they are available to me later in the script.

Initially I tried a for do loop like this:

for trigger_files in $param; do
    app1=(basename "{trigger_files}" .trg).dat
done

But that only gives the first occurrence app1=ABC1099R

I need to have all occurrences saved to their own individual variable. There could be potentially 20 filenames, but I figured I just needed to get it working with two, therefore it would be able to handle many.

  • Correction - the find command produced the values ABC1099R.trg and ABC1099C.trg - I also need to remove the .trg from the values in the variables, but I think I can handle that I just really need suggestions on how to take that array and assign to individual variables. Thanks. – user50228 Oct 29 '13 at 23:38
  • Hi and welcome to U&L, we're glad to have you here. Instead of leaving the corrections comment you can edit your post as you like and fix it that way instead. It's easier to read for future visitors and makes the site cleaner and easier to navigate. Let us know if you have any questions 8-) – slm Oct 30 '13 at 1:55
  • Since you're new to Bash scripting you might want to spend some time familarizing yourself with its pitfalls: mywiki.wooledge.org/BashPitfalls – slm Oct 30 '13 at 2:05
9

If you're using , please consider doing it the bash way.

Your array would by obtained thus (no external find command):

params=( *${file_name}*.trg )

and then you loop through the array like so:

for file in "${params[@]}"; do
    echo "I'm happily reading the beautiful file $file"
done

If you want to remove the .trg extension and replace it with .banana:

for file in "${params[@]}"; do
    echo "My gorilla loves the file ${file%.trg}.banana"
done

This method, with proper quoting, will be 100% safe regarding file with funny symbols in their names.

Remark. Always use globbings with either shopt -s nullglob or shopt -s failglob.

2

You could declare param as an array instead of as a string:

declare -a param=(`find *${file_name}*.trg`)

Now you can access the individual values using an index as if they where individual variables. For example:

echo ${param[1]}

will print ABC1099C.trg (the second element in the array. The index starts with 0).

If you want to get rid of the file extension, you could try ${param[1]%.*}

To find out more about arrays take a look here:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.