-1

How do i go about this.

For example, Lets say i have a script that:

pushd '\\PATH1\PATH2\SCANDIR';
find . -mtime -120 -name "*.exe" -exec stat -c "%n %.19z" {} \;

What i would like to be able to do, Is run this script. But set -mtime

i.e. in console i would like to type in:

scriptname.sh -120

This would set -mtime to -120. How does one go about constructing the line to accept a parameter on the script.

  • Also see In Bash, when to alias, when to script, and when to write a function? — this is a good candidate for a function rather than a script. – Wildcard Mar 15 '16 at 10:02
  • Thanks, I already have used this in a Alias, I used this as an example as it was easier to understand than posting a whole script. All i was trying to find out what the correct terminology and method to pass an argument from command line to the script.

    The script itself search 15 directories for specific types where a fileversion is greater than previous (Stored content) and dates fall into a specific range. I wanted to pass some arguments across, Date range, File type and additional search directory. This makes using an Alias very easy. RunAliasname Param1 Param2 Dir1

    – Dave Hamilton Mar 15 '16 at 11:27

1 Answers1

2

well that can be done easily. Try this.

#!/bin/bash
parm="$1"
find . -mtime "${parm}" -name "*.exe" -exec stat -c "%n %.19z" {} \;

now you can pass it like

./scriptname.sh -120 
./scriptname.sh +120
Wildcard
  • 36,499
  • If . is hardcoded in the script and you run it with ./, you'll only search the directory the script is stored in. – Wildcard Mar 15 '16 at 10:03
  • Thanks very much, the =$1 is the one. Am assuming on this logic.

    If i have $1 $2 $3 $4 Then if i pass 4 paramers after the script name then it would populate them in order from 1-4? i.e. parm1="$1" parm2="$2" parm3="$3" parm4="$4" These would be populated in order of: scriptname.sh 1 2 3 4

    – Dave Hamilton Mar 15 '16 at 10:07
  • 2
    There's no reason not to just use "$1" directly. Nothing is accomplished by assigning it to parm first. – Wildcard Mar 15 '16 at 11:32
  • I think of otherwise unnecessary assignments like parm="$1" as documentation, although using a more descriptive name is a good idea in that case. – chepner Mar 15 '16 at 15:39
  • 1
    calling it parm{1,2,3,4} is not documentation, it provides no more information to the reader than just using "$1" etc directly. Use meaningful variable names if you want your script code to be self-documenting. mtime="$1" is meaningful. parm1="$1" is not. – cas Mar 15 '16 at 21:42