0

I want to make a non-interactive shell script,where I can give options at the beginning of the execution of the script.

Where I can hard-code the various actions to be taken when different inputs are provided by user.

for example:

Below should perform some action on target.txt

user@/root>myPlannedScript -p targer.txt   

Below should perform some other actions on the target.txt

user@/root>myPlannedScript -a targer.txt  

For example:

cat tool performs various actions when different options are given. I want my script to act like this:

:/root> cat --h  
Usage: cat [OPTION] [FILE]...  
Concatenate FILE(s), or standard input, to standard output.  

  -A, --show-all           equivalent to -vET  
  -b, --number-nonblank    number nonblank output lines  
  -e                       equivalent to -vE  
  -E, --show-ends          display $ at end of each line  
  -n, --number             number all output lines  
  -r, --reversible         use \ to make the output reversible, implies -v  
  -s, --squeeze-blank      never more than one single blank line  
  -t                       equivalent to -vT  
  -T, --show-tabs          display TAB characters as ^I  
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • 1
    What's you problem, exactly ? – Mali May 27 '14 at 15:19
  • I have a script where I have used user interaction (read tool)for providing target.txt, Now I want to execute this command in one go by providing the target.txt file name in a single line. –  May 27 '14 at 15:22
  • 3
    The canonical answer is at http://mywiki.wooledge.org/BashFAQ/035 – Charles Duffy May 27 '14 at 15:36

1 Answers1

0

query.sh

#!/bin/bash
if [ $# -eq 0 ]
then echo do one thing
else echo do other thing
fi

"query.sh" => do one thing

"query.sh anythingYouPut" => do other thing ;oP

but if you really want a parameter for each action

#!/bin/bash
if [ -z "$1" ]
then
  echo do nothing
else
  if [ $1 -eq 1 ]
  then
    echo do one thing
  fi
  if [ $1 -eq 2 ]
  then
    echo do other thing
  fi
fi

"query.sh" => do nothing

"query.sh 1" => do one thing

"query.sh 2" => do other thing

robin
  • 159
  • 5