passing runtime arguments to a shell script...

hi I am new to shell programming.....my question is while running one of my shell program it stops in between to accept input from the user and proceeds furthur after giving input....I want to know whether I can set this input through some files so that the shell acript reads the input from the file and will not prompt the user to input the required data...the input is a text...is there any other possibilities using pipes....

thanks in advance :slight_smile:

This is basic file redirection.

./script.sh < input-file

will this command automatically takes the input from the input-file...should i append this line to the end of the script....thanks for ur reply...

Yes, it will automatically take the input from the input file. It's not telling the program to do so, the shell automatically feeds the file into it's input when it creates the process. You can add that to the end of your script, if that's what you wish the script to do. You can even feed the output of one program into the input of another with pipes:

./script.sh | ./script2.sh

thanks a lot:-)

Hi,

Have you guys anyone worked on CA workload, i have rqmnt that UNIX script should accept the parameter passed through the CA workload and run the process. To explain in the detail CA workload will tell which files is being placed on the server if file1 is placed it needs to process file1 and if file2 is placed it has to process it and so on. Any help would be appreciated.

You can use the getopts command to handle command line arguments.

Thanks Padow for your response, Is the Getopts command reads the parameter from CA workload. Can you give me the syntax or example how it works?

Here is a sample script I use to rsh commands (I know I should be using SSH instead) to multiple hosts. The hosts to be used and the command to be sent is received from the command line:

#!/bin/ksh

. /usr/sample/etc/unixadm.env  #Get host list variables

function printusage {
  echo "Usage: checkhosts.ksh [npsu] -c command"
  echo "       n:  AIX non-production"
  echo "       p:  AIX production"
  echo "       s:  SUN production"
  echo "       u:  SUN non-production"
}

while getopts ":npsuc:" opt
do
        case $opt in
        c) cOmmand="$OPTARG" ;;
        n) HOSTS=$AIXNP ;;
        p) HOSTS="$AIXPROD $HOSTS" ;;
        s) HOSTS="$SUNPROD $HOSTS" ;;
        u) HOSTS="$HOSTS $SUNNP" ;;
        ?) printusage
                exit 1;;
        esac
done
if [[ -z $cOmmand || -z $HOSTS ]]; then  #Check to make sure that there are hosts and a command to use
  printusage
  exit 2
fi

if ! echo $cOmmand | grep [a-zA-Z]; then  #Another validity test
  echo "invalid command: $cOmmand"
  printusage
  exit 2
fi

for i in $HOSTS; do
  echo "===== $i ====="
  if echo $i | grep -qiE "dev|qa|test"; then
    i=$i.qadomain.com
  fi
  rsh $i $cOmmand
done

On the getopts line:

  • Having a colon first means that we will process our own errors
  • Each letter listed is a valid switch
  • The colon after the c means that a value should accompany the switch on the command line

A valid command to execute this script would be :

./checkhosts.ksh -nc "lssrc -s net-snmp"

Hi Paddow,

Thanks for you script. Can you provide me the sample script which should accept only one parameter send through the command line. Also i need to check the argument for following condition

if $arg1 = 'ABC'
concatenate 'ABC' to filename
else
if $arg1 = 'DEF'
concatenate 'DEF' to filename
end-if


case $1 in
     ABC|DEF) filename=${filename}$1 ;;
     *) echo "What do I do now?" ;;
esac