How to use a variable in a filename and script?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
eddie3000
Level 3
Level 3
Posts: 136
Joined: Mon Jun 24, 2013 2:11 pm

How to use a variable in a filename and script?

Post by eddie3000 »

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
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
jimallyn
Level 19
Level 19
Posts: 9075
Joined: Thu Jun 05, 2014 7:34 pm
Location: Wenatchee, WA USA

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

Post by jimallyn »

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
“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 3
Level 3
Posts: 136
Joined: Mon Jun 24, 2013 2:11 pm

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

Post by eddie3000 »

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

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

Post by lmuserx4849 »

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 3
Level 3
Posts: 136
Joined: Mon Jun 24, 2013 2:11 pm

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

Post by eddie3000 »

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.
Locked

Return to “Scripts & Bash”