Operating System - HP-UX
1752790 Members
6074 Online
108789 Solutions
New Discussion юеВ

Re: Scripting clarification

 
Omprakash_2
Frequent Advisor

Scripting clarification

Hi,

I just want to know how to read a file through script. My requirement is i want to take backup if a file contain the fifth line as "yes". If "Yes" word is there means i need to fire a backup. "No" means i should not.

I am already having a script for only taking the backup. I want to execute through command.
18 REPLIES 18
Mark McDonald_2
Trusted Contributor

Re: Scripting clarification

Is the 5th line only "Yes" or "No" OR do you have BACKUP=Yes ?

there are several ways to read the file in to a script.

eg:

cat file | while read LINE
do
:
:
done

you can either count to the 5th line, or look for a string BACKUP
Omprakash_2
Frequent Advisor

Re: Scripting clarification

Hi,

The fifth line is only "Yes" or "No". Can you provide any sample script for the same.
Mark McDonald_2
Trusted Contributor

Re: Scripting clarification

#!/usr/bin/ksh
FILE=/path/filename
count=1
cat $FILE | while read LINE
do

if [[ LNUM -eq 5 && LINE = "Yes" ]]
then
do_backup
commands here
fi

(( count += 1 )) <- check syntax
done
Mark McDonald_2
Trusted Contributor

Re: Scripting clarification

SORRY

change this line:
if [[ $count -eq 5 && $LINE = "Yes" ]]
Suraj K Sankari
Honored Contributor

Re: Scripting clarification

Hi,
here one more script

[root@rspc521 tmp]# cat srvlist
server1
server2
server3
server4
Yes
server6
server7

[root@rspc521 tmp]# cat myscript
ans=`sed -n '5,5p' srvlist`
if [ $ans=="yes" ]
then
echo " your backup script"
fi


Suraj
Omprakash_2
Frequent Advisor

Re: Scripting clarification

Hi suraj,

I can understand well about your script.

But i am getting following error,

# ./script2
./script2[3]: [sed: not found.

below mentioned script for your convenience.

# pg script1

#!/usr/bin/sh
server1
server2
server3
server4
Yes
server5
server6
--------------------------------------------
# pg script2
#!/usr/bin/sh
ans='sed -n'5,5p'script1'
if [$ans=="Yes"]
then
echo "Your current Location"
pwd
date
fi

--------------------------------------------


Frank de Vries
Respected Contributor

Re: Scripting clarification

Hi,

Script is good,
but in your environment it can find the
sed editor, or you do not have one
(but that last would be strange)

usual the path is
[root@orasrv1:]/root<>>> which sed
/usr/bin/sed
[root@orasrv1:]/root<>>>

do
export PATH=/usr/bin:$PATH
in your script or session
before running sed command

have fun
Look before you leap
T G Manikandan
Honored Contributor

Re: Scripting clarification

if test `cat /tmp/a.sh|awk 'NR==5 {print $0}'` = "yes"; then echo "fire backup"; else echo "please dont fire backup"; fi
Omprakash_2
Frequent Advisor

Re: Scripting clarification

Hi suraj / Frank,

I am very new to scripting language. I have tried like you said but its still not working i have entered like below required your suggestion..

#!/usr/bin/sh
ans='sed -n'5,5p'script1'
if [$ans=="Yes"]
then
echo "Your current Location"
pwd
date
fi
export PATH=/usr/bin:$PATH
--------------------------------------------

I need to execute this urgent manner...