1

I'm really stuck on my scripting because of this problem. I need my script to accept arguments and give me the result but since i have spaces it only displays the first word. eg.

cat open.sh
echo My name is $1

export start=`date +"%b %d %k:%M:%S %Y"`

[oracle@spiderman scripts]echo $start
Jul 01 3:03:18 2016

[oracle@spiderman scripts]$sh open.sh `echo $start`
The time is Jul

I need the output as The time is Jul 01 3:03:18 2016.

I need to pass the same output to another complex script.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Super
  • 67

1 Answers1

3

You should use command substitution $() around date like,

export start=$(date +"%b %d %k:%M:%S %Y")
export end=$(date +"%b %d %k:%M:%S %Y")

and change open.sh with

echo The time is "$1" and end time is "$2"

(Note : Always quotes your variable, see this)

and run your script with quoted argument like this,

$ sh open.sh "$start" "$end"
The time is Jul 01 20:24:03 2016 and end time is Jul 01 22:19:25 2016
Rahul
  • 13,589
  • Thanks for the answer, I have multiple variables to enter. eg start and end time, so can i do it in this way – Super Jul 01 '16 at 12:17
  • export start=$(date +"%b %d %k:%M:%S %Y") – Super Jul 01 '16 at 12:17
  • @Super Yes, $@ treats multiple arguments – Rahul Jul 01 '16 at 12:18
  • export start=$(date +"%b %d %k:%M:%S %Y") export end=$(date +"%b %d %k:%M:%S %Y") -- change open.sh with echo The start time is "$@" echo The end time is "$@" will this work ? – Super Jul 01 '16 at 12:18
  • @Super see my updated answer – Rahul Jul 01 '16 at 12:23
  • My output is not readable this way..

    export start=$(date +"%b %d %k:%M:%S %Y") export end=$(date +"%b %d %k:%M:%S %Y")

    scripts]$sh open.sh $start $end the start time is Jul 01 5:20:40 2016 Jul 01 5:20:41 2016 the end time si Jul 01 5:20:40 2016 Jul 01 5:20:41 2016

    scripts]$cat open.sh echo the start time is "$@" echo the end time si "$@"

    – Super Jul 01 '16 at 12:25
  • @Super see my updated answer, I have used $1 & $2 for first and second argument respectively. – Rahul Jul 01 '16 at 12:26
  • @Super If that helped you, then please consider accepting answer by clicking on right mark below up/down arrow. – Rahul Jul 01 '16 at 12:31
  • Hi Rahul,

    I works well in a simple script but it fails in my scenario.

    the loop i run below goes to multiple database's on different hosts. for n in cat $TXTDIR/hostname.txt eg:- node1 - dbname (TEST1) node2 - dbname (TEST2)

    – Super Jul 01 '16 at 13:04
  • oswbb]$for n in cat $TXTDIR/hostname.txt

    do ssh oracle@$n "source $BASEDIR/.setenv; export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/jdk/bin:$PATH; sh /home/oracle/soft/oswbb/name.sh "$start" "$end"" done

    Invalid date: Jul -e 01 -s Invalid date: 01 -s -A /orasoft/scripts/logs/perf_owatcher_spiderman.glen.log

    [oracle@spiderman oswbb]$cat name.sh java -jar /home/oracle/soft/oswbb/oswbba.jar -i $OSWATCHLOGS -P oswatcher_html_$n -6 -7 -8 -b $1 -e $2 -s -A $LOGDIR/perf_owatcher_hostname.log

    – Super Jul 01 '16 at 13:05