Need help with array substitution

Hello all,
I have following piece of code which is working fine if executed standalone -

date=$1
set -A max_month 0 31 28 31 30 31 30 31 31 30 31 30 31
eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!')
(( year4=year%4 ))
(( year100=year%100 ))
(( year400=year%400 ))
if [ \( $year4 -eq 0 -a \
        $year100 -ne 0 \) -o \
     $year400 -eq 0 ]
then
 set -A max_month 0 31 29 31 30 31 30 31 31 30 31 30 31
fi
day=$((day+1))
echo $day ${max_month[$month]}
if [ $day -gt ${max_month[$month]} ]
then
  day=1
  month=$((month+1))
  if [ $month -gt 12 ]
  then
    year=$((year+1))
    month=1
  fi
fi
new_date=$(printf "%4.4d%2.2d%2.2d" $year $month $day)
echo $new_date

When I try to embed it into following code highlighted in red it throws error, obviously I replaced date=$1 to julian_date_14 -

#!/bin/bash
cd
unset project_env
cd /wload/baot/home/baotasa0/UKRB_UKBE/sandboxes/EXTRACTS/UK/RB/UKBA/ukrb_ukba_pbe_acq
. ab* . >> project_setup.log 2>&1
echo unset the environment is doNe
cd /wload/baot/app/data_abinitio/serial/uk_cust
param1=$1
param2=$2
param3=$3
email=$4
header_date_14=$(m_dump /wload/baot/app/data_abinitio/serial/uk_cust/ukrb_ukba_acnt_bde27_src.dml $param1 | head -35)
hdr_dt_14=$(echo "$header_date_14" | awk '$1=="bdfo_run_date" {print $2}')
julian_date_14=$(m_eval '(date("YYYYMMDD"))( unsigned integer(2)) '$hdr_dt_14'') 2>&1
header_date_15=$(m_dump /wload/baot/app/data_abinitio/serial/uk_cust/ukrb_ukba_acnt_bde27_src.dml $param2 | head -35)
hdr_dt_15=$(echo "$header_date_15" | awk '$1=="bdfo_run_date" {print $2}')
julian_date_15=$(m_eval '(date("YYYYMMDD"))( unsigned integer(2)) '$hdr_dt_15'')
header_date_16=$(m_dump /wload/baot/app/data_abinitio/serial/uk_cust/ukrb_ukba_acnt_bde27_src.dml $param3 | head -35)
hdr_dt_16=$(echo "$header_date_16" | awk '$1=="bdfo_run_date" {print $2}')
julian_date_16=$(m_eval '(date("YYYYMMDD"))( unsigned integer(2)) '$hdr_dt_16'')
echo $julian_date_16
if [ "$julian_date_14" = "$julian_date_15" -a "$julian_date_15" = "$julian_date_16" ]
then
echo all are same
else
echo check the file date please
fi
DATE=`echo $julian_date_14 | cut -c8-9`
Date_minus_1=`expr $DATE - 1`
DATE_1=`echo $julian_date_14 | cut -c2-7`
DATE_FINAL="$DATE_1$Date_minus_1"
echo $DATE_FINAL

Error is below -

./auto1.sh: line 70: set: -A: invalid option
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

Any help will be greatly appreciated.
Thank you in advance!

See my comment in your post on using "urgent" in the topic, thanks.

Your first script uses ksh syntax to declare an array with set -A . In your second script you use a shebang to call bash, which has a different syntax to declare an array like:

$ declare -a myarray=(eins zwei drei)
$ echo ${myarray[1]}
zwei

#or

$ myarray=(eins zwei drei)
$ echo ${myarray[2]}
drei
1 Like

Thanks again, I am just curious that I includedksh library in the starting of script by writing below code -
#!/bin/bash
#!/bin/ksh
#!/bin/sh
#!/bin/csh
Then why it picked up only bash syntax :frowning:

The #! in exactly the first two character positions of a script tell the command interpreter which program to run to read and execute the script. All the other lines are pure comments and thus ignored. Nothing to do with libraries.

Since it is ksh code, if you put:

#!/bin/ksh

At the very beginning of your script then it should run fine, provided that you make the script executable ( chmod +x ) and run it like /path/to/script , or like script if the path leading to the script is in your search path.

If you start the script like

bash script

then that will not work, since the first line is simply a comment and ksh will not be used.

You can also make sure ksh is used by calling it like:

ksh script

--

Actually it is the first two bytes and they inform the OS' program loader that the file is a script and that the remainder of the line specifies which command interpreter to run, with the script name as argument. This command interpreter is subsequently started and it then reads the script and typically interprets this first line - that starts with #! (the "shebang") - as comment, because it starts with # .

2 Likes

Need to memorize that...

In addition it might be worth adding that only absolute pathes are allowed here. This:

#! /path/to/something

will work, whereas

#! ../../something

will not, even if ../../something would correctly resolve to the executable.

I hope this helps.

bakunin

Actually it is allowed to use a relative path in the shebang, but there really is no practical use for it..

$ cat testscript
#! /bin/bash -x
echo hello
./testscript
+ echo hello
hello
$ cat testscript2
#! bin/bash -x
echo hello
( dir=$(pwd); cd /; "${dir}"/testscript2 )
+ echo hello
hello