0

hello all newbie here :D

i am working on a project right now which involves a script that handles a *.dat file.The script runs under specific commands inputed by user

my problem is that i cant figure out how to "grab" the arguments from the command to "import" them to the script

here are some examples from the specific commands :

  1. ./tool.sh -f <file>

  2. ./tool.sh -f <file> -id <id>

  3. ./tool.sh --born-since <dateA> --born-until <dateB> -f <file>

where tool.sh is the scripts name, <file> is the *.dat file, <date> is just a date in this format YYYY-MM-DD

so as you can see there are both shortwords and longwords. I know that the getopts command is tricky to handle the long ones. How can i handle all of them using a "single" piece of code?

edit: i need to use the arguments and the conditional expressions for a case using the "if"

so could i just do :

#!/bin/bash 
getopts f a
if [ $1 == "something" && $2 == "something2" && $a == "f" ]; then
....
elif [ $1 == "something3" && $2 == "something4" && $a == "f" ]
....
else 
....
fi

would that be correct?

1 Answers1

0

In a bash script you can access command line arguments via order like so:

echo $1 # the first argument
echo $2 # the second argument
yonizxz
  • 26
  • 2
  • This is buggy. If run with `./tool.sh '*'`, it would print all filenames in the current directory, not a `*`. Needs to be `echo "$1"`, with the quotes. And even then, it doesn't help the OP with parsing arguments that are named rather than positional. – Charles Duffy Oct 11 '16 at 21:45