How to use a variable in a filename and script?

Forum rules
Before you post please read how to get help
eddie3000
Level 2
Level 2
Posts: 50
Joined: Mon Jun 24, 2013 2:11 pm

How to use a variable in a filename and script?

Postby eddie3000 » Wed Mar 08, 2017 5:48 am

Hello there!

I am still very poor at scripting. Just very basic stuff. I need a bit of help writing a script.

I have to execute a command with fixed options like this "command -a -b file01" on a very large amount of files (file01, file02,file02....). The filenames can be in numeric format if that makes things easier. With my very basic knowledge I would have to make a script with as many lines as files that need the command to be applied to. Could a kind person help me with a script to do just that?

Thank you very much

User avatar
jimallyn
Level 16
Level 16
Posts: 6937
Joined: Thu Jun 05, 2014 7:34 pm
Location: Wenatchee, WA USA

Re: How to use a variable in a filename and script?

Postby jimallyn » Wed Mar 08, 2017 5:58 am

I think you probably want to use a for loop. Download "The Linux Command Line" and have a look at section 33.

https://downloads.sourceforge.net/proje ... irror=svwh
Image

“If the government were coming for your TVs and cars, then you'd be upset. But, as it is, they're only coming for your sons.” - Daniel Berrigan

eddie3000
Level 2
Level 2
Posts: 50
Joined: Mon Jun 24, 2013 2:11 pm

Re: How to use a variable in a filename and script?

Postby eddie3000 » Wed Mar 08, 2017 9:06 am

Wow! Thank you very much indeed. That looks like a very interesting manual. I will read it.

I managed to solve my problem by the way. I used a loop using while instead of for, but it's just the same.

Code: Select all

#!/bin/bash
         COUNTER=1
         while [  $COUNTER -lt 10000 ]; do
             whatever it is I want to do
             COUNTER=$((COUNTER+1))
         done

lmuserx4849
Level 4
Level 4
Posts: 489
Joined: Wed Dec 17, 2014 2:55 am

Re: How to use a variable in a filename and script?

Postby lmuserx4849 » Thu Mar 09, 2017 1:12 am

Sample:

Code: Select all

#!/bin/bash
#======
# Execute a command with fixed options like this "command -a -b file01" on a  very large amount of files (file01, file02,file02....)
#======
set -u                             # unset variables are an error
declare -a cmdArgs=()  # command options
declare -- f=''                 # work variable for file name

cmdArgs+=('-a')             # add an option
cmdArgs+=('-b')

cd ${HOME}/thepath/    # change to directory

for f in ./pattern*; do      # replace pattern; see info on globbing
  # echo "$f"                  # uncomment to test you are getting the right files
 
  if [[ -f "${f}" ]]; do         # test - working with file
    # /pathto/cmd "${cmdArgs[@]}" "${f}"  # uncomment to execute your command
   
    if [[ "${?}" -ne 0 ]]; then                        # Check return status of command; Could also be written as (( $? != 0 ))
      printf -- 'command failed with exit status %d - %s\n' "${?}"  "${f}"  # could be a simple echo
      #exit 2                                                # uncomment to get out
    fi
  fi
done


1. The reason the command options are put into an array is due to how Bash parses the command line. If you do it this way, it will always work.
See FAQ: http://mywiki.wooledge.org/BashFAQ/050

2. Always quote filenames.
For advanced details: https://www.dwheeler.com/essays/fixing- ... names.html

3. For more information about patterns or globbing:
https://www.gnu.org/software/bash/manua ... n-Matching

This pattern matches files that begin with 2 digits: [0-9][0-9]*
Patterns (globs), strings, and regular expressions are different things.

4. If the pattern does not match any file, the pattern itself is returned unless nullglob is set (shopt -s nullglob).
The file test (-f) will catch.

5. To see available bash tests, type: help test or see bash man page or reference
https://www.gnu.org/software/bash/manua ... xpressions

6. There are multiple ways to accomplish your task. You'll often see the find command used by itself with -exec or xargs or as input piped or redirected to a while statement. For example:

Code: Select all

   
while read fName fSize; do
  printf -- '%s %d\n' "${fName}" "${fSize}"
done < <(find . -type f -name "*.sh" -printf '%P %s\n')

See process substitution:
https://www.gnu.org/software/bash/manua ... bstitution

7. Nice commands will tell you what they do if they fail. :-) Not all commands are nice. :-( For example, if you do a man find and scroll down almost to the bottom, there will be a section "EXIT STATUS". Look for something like that in a command's man page. It is uncommon for a command not to have a man page. Learn the man system.

8. Already mentioned, the Linux Command Org is an excellent resource. He has a "short" tutorial that walks through building a single script. Under resources he has a script that creates a bash template, so all your scripts are consistent. He's one of the few places that talks about creating help and traps for your script. If you create a lot of scripts and store them in $HOME/bin, in the future you may wonder why you created it :-) A quick cmd --help or cmd -h is nice ;-)

eddie3000
Level 2
Level 2
Posts: 50
Joined: Mon Jun 24, 2013 2:11 pm

Re: How to use a variable in a filename and script?

Postby eddie3000 » Wed Apr 26, 2017 7:03 am

Sorry for the late reply, but thank you very very much. You information has turned out to be very helpful indeed.

I have started to mess a bit more with scripting and I'm quite enjoying it. I have also recently purchased a raspberry pi, and I hope to learn and eventually be a good script writer. Maybe I'll learn C afterwards? Mmmm...

Thank you very much.


Return to “Scripts & Bash”