Read a control file

Hi I have the below .sh script which receives a argument ftpClient=$1 . This argument i am trying to read from a control file called ftpProperties.control.
Based on the argument input(client_ftx OR client_ptm),the script shall read the particular entries from the control file like ftp host,user,pwd and connect.
I have a solaris box where i use bash. The awk command is failing (error attached).This script works fine in Linux but fails when i run from solaris.
Any help is appreciated.

(1)Script
******

#!/bin/ksh

YESTERDAY=`TZ=GMT+29 date +%Y%m%d`
ftpClient=$1
ftpPropertyFile=/app/temp/ftpProperties.control
cat $ftpPropertyFile|sed -n "/$ftpClient/,/<<EOF/p"|awk -v 'RS=\n\n' '1;{exit}'|tail -n +2 > $ftpClient.txt
. ./$ftpClient.txt
echo "start of script"
ftp -nv $HOST <<END_SCRIPT
user $USER $PASSWORD
cd $SOURCEPATH
lcd /data/file
get $FILENAME1$YESTERDAY
bye
END_SCRIPT

echo "end of script"

(2)Control File
************

[client_ftx]
HOST=ftpprod.com
USER=sam
PASSWORD=abc123
SOURCEPATH=/samdir
FILENAME1="stor_cax."


[client_ptm]
HOST=ids.com 
USER=ftpuser
PASSWORD=xyz234
SOURCEPATH=/ftpdir
FILENAME1="previousday.srv"

(3)Error
*******

usage: tail [+/-[n][lbc][f]] [file]
       tail [+/-[n][l][r|f]] [file]
start of script
Not connected.
Not connected.
awk: syntax error near line 1
awk: bailing out near line 1
Local directory now /data/file
Not connected.
end of script

Try :

$ cat testfile
[client_ftx]
HOST=ftpprod.com
USER=sam
PASSWORD=abc123
SOURCEPATH=/samdir
FILENAME1="stor_cax."


[client_ptm]
HOST=ids.com 
USER=ftpuser
PASSWORD=xyz234
SOURCEPATH=/ftpdir
FILENAME1="previousday.srv"
$ cat tester
#!/bin/bash


control_file="testfile"

while read line
do

	[[ "$line" =~ ^HOST ]] && HOST="${line#*=}"
        [[ "$line" =~ ^USER ]] && USER="${line#*=}"
        [[ "$line" =~ ^PASSWORD ]] && PASSWORD="${line#*=}"
        [[ "$line" =~ ^SOURCEPATH ]] && SOURCEPATH="${line#*=}"

 if  [[ "$line" =~ ^FILENAME1 ]]; then

	FILENAME1="${line#*=}"
       

       # So now username password etc are parsed from control file 
       # Process whatever you want here...
  
       echo $HOST $USER $PASSWORD $SOURCEPATH $FILENAME1

 fi
 
done < "$control_file"
$ bash tester
ftpprod.com sam abc123 /samdir "stor_cax."
ids.com ftpuser xyz234 /ftpdir "previousday.srv"

Hi I ran the below script. It basically reads the entire control file and displays. However My requirement is to pass into the main script an argument like client_ftx or client_ptm and the script should refer the ftpProperties.control file and read the relevant section details like ftp host,user,pwd. The script then shall connect to the respective FTP server based on this.

#!/bin/bash
control_file=/app/temp/ftpProperties.control
while read line
do

        [[ "$line" =~ ^HOST ]] && HOST="${line#*=}"
        [[ "$line" =~ ^USER ]] && USER="${line#*=}"
        [[ "$line" =~ ^PASSWORD ]] && PASSWORD="${line#*=}"
        [[ "$line" =~ ^SOURCEPATH ]] && SOURCEPATH="${line#*=}"

 if  [[ "$line" =~ ^FILENAME1 ]]; then

        FILENAME1="${line#*=}"


       # So now username password etc are parsed from control file
       # Process whatever you want here...

       echo $HOST $USER $PASSWORD $SOURCEPATH $FILENAME1

 fi

done < "$control_file"

Fine, use this code

#!/bin/bash

# Use your $1 arguments here
client="client_ftx"

# Your FTP Control file
control_file="controlfile"

while read line
do

        [[ "$line" =~ ^\[    ]] && CLIENT="${line//[\[\]]/}"

	[[ "$line" =~ ^HOST  ]] && HOST="${line#*=}"
        [[ "$line" =~ ^USER  ]] && USER="${line#*=}"
        [[ "$line" =~ ^PASSWORD ]] && PASSWORD="${line#*=}"
        [[ "$line" =~ ^SOURCEPATH ]] && SOURCEPATH="${line#*=}"

 if [[ "$CLIENT" == "$client" ]] && [[ "$line" =~ ^FILENAME1 ]]; then

	FILENAME1="${line#*=}"
       

       # So now username password etc are parsed from control file 
       # Process whatever you want here...
  
       echo $CLIENT $HOST $USER $PASSWORD $SOURCEPATH $FILENAME1

 fi
 
done < "$control_file"
$ bash tester
client_ftx ftpprod.com sam abc123 /samdir "stor_cax."

I am not happy, after giving hints to parse file, you did not try anything to meet your requirement.

1 Like

You seem to already have the rest. Plug in your ftp -nv $HOST <<END_SCRIPT where needed.

Thanks Friends.

Meanwhile in my original post i wanted to highlight the issue i faced using awk and tail in solaris . Can you guys help me to point out if anything is missed here ? Error is attached as well

#!/bin/bash
control_file=client_ptm
ftpPropertyFile=/app/temp/ftpProperties.control
cat $ftpPropertyFile|sed -n "/$control_file/,/<<EOF/p" |awk -v 'RS=\n' '1;{exit}'|tail -n +1 > $ftpClient.txt
. ./$ftpClient.txt

ERRROR:
******

usage: tail [+/-[n][lbc][f]] [file]
       tail [+/-[n][l][r|f]] [file]
start of script
Not connected.
Not connected.
awk: syntax error near line 1
awk: bailing out near line 1
Local directory now /data/file
Not connected.
end of script

change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk

Thanks All