bash - how to pass arg values to shell script within in a cronjob -


i have cron entry looks works: (passing in 5 inputs)

30 10 * * 5 sh /home/test.sh hostnm101.abc /mypath/dir test foobar f008ar >> /logs/mytst.log 2>&1 

i want change store inputs (4,5) foobar , f008ar in separate file , read in script test.sh ($4,$5)

test.sh

#!/bin/bash  if [ $# != 5 ];     exit 1 fi  dirdt=`date '+%y%m%d'` tstdir=$2/test/$dirdate [ ! -d "$tstdir" ] && ( mkdir "$tstdir" || { echo 'mkdir command failed'; exit 1; } )  perl /home/dev/tstextr.pl -n $1 -b $2 -d $tstdir/ -s $3 -u $4 -p $5 -f $dirdt 

is there easy way within cron (2) input values? thanks.

alright try way.

1) create separate file /mypath/dir/login.info content (username/password in separate lines):

foobar f008ar 


2) modify test.sh this:

#!/bin/bash if [ $# != 4 ];     exit 1 fi  dirdt=`date '+%y%m%d'` tstdir=$2/test/$dirdate [ ! -d "$tstdir" ] && ( mkdir "$tstdir" || { echo 'mkdir command failed'; exit 1; } )  ifs=" " arr=( $(<$2/$4) ) #echo "username=${arr[0]}   password=${arr[1]}"  perl /home/dev/tstextr.pl -n $1 -b $2 -d $tstdir/ -s $3 -u ${arr[0]} -p ${arr[1]} -f $dirdt 


3) have cron command this:

30 10 * * 5 sh /home/test.sh hostnm101.abc /mypath/dir test login.info >> /logs/mytst.log 2>&1 

summary

  • ifs stands internal field separator (ifs) in bash

i using this:

ifs=" " 

which means make new line character field separator (since storing username , password in 2 separate lines). , line read file /mypath/dir/login.info array:

arr=( $(<$2/$4) ) 
  • first line (username) read $arr[0]
  • second line (password) read $arr[1]

you can echo check read content:

echo "username=${arr[0]}" echo "password=${arr[1]}" 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -