cancel
Showing results for 
Search instead for 
Did you mean: 

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
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...
Omprakash_2
Frequent Advisor

Re: Scripting clarification

Hi Manikandan,

As you said i have edit as below, i am getting output as well. But if i edit script1 without the word "Yes". Its provide me the same output.

# pg script1
#!/usr/bin/sh
server1
server2
server3
server4
server5
server6
(EOF):
--------------------------------------------
# pg script2
#!/usr/bin/sh
#ans='sed -n'5,5p'script1'
if test 'cat /script1'NR==5{print$0}="Yes"
then
echo "Your current Location"
pwd
date
else
echo "stop"
fi

(EOF):
--------------------------------------------
OUTPUT:
-------
# ./script2
Your current Location
/
Fri Apr 3 14:15:22 IST 2009

-------------------------------------------
T G Manikandan
Honored Contributor

Re: Scripting clarification

`cat /tmp/a.sh|awk 'NR==5 {print $0}'` = "yes"

Please check the quote before cat and after $0}

These quotes are command substitution quotes and not normal single quote.
T G Manikandan
Honored Contributor

Re: Scripting clarification

I dont see awk statement in your copied version and no spaces too, it should be

if test `cat /script1 |awk 'NR==5 {print$0}'` = "Yes"
T G Manikandan
Honored Contributor

Re: Scripting clarification

Attaching script2
Omprakash_2
Frequent Advisor

Re: Scripting clarification

Hi Manikandan,

Problem fixed because of you, Thanks a ton its working fine !!!
Omprakash_2
Frequent Advisor

Re: Scripting clarification

Thanks Suraj/Frank & Manikandan...
T G Manikandan
Honored Contributor

Re: Scripting clarification

wow, it fixed but you give 1 point?
Dennis Handly
Acclaimed Contributor

Re: Scripting clarification

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

This says the word "[sed" is not found. You seem to have some missing spaces.

ans='sed -n'5,5p'script1'
if [$ans=="Yes"]

The first problem is the attempted use (as TG said) of archaic ``, instead of the easy to read $()
ans=$(sed -n '5,5p' script1)
Then you are missing that space (as TG said):
if [ "$ans" = "Yes" ]; then

# pg script1
#!/usr/bin/sh

This isn't a script, it is a data file.

>TG: if test `cat /script1 |awk 'NR==5 {print$0}'` = "Yes"

No need for evil cat or test, or ``:
if [ "$(awk 'NR==5 {print $0; exit}' script1)" = "Yes" ]; then
T G Manikandan
Honored Contributor

Re: Scripting clarification

Thanks Dennis for those corrections.