LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash shell program question (http://www.linuxquestions.org/questions/programming-9/bash-shell-program-question-893073/)

rsatyandra 07-21-2011 11:28 PM

bash shell program question
 
Hello Guru's ,

Could you please help me on shell programming . I am new to this programming kindly give me your valuable feedback on this query .

I Have 3 folders ,in each folder have one xxx.sh program. based on the my result above 3 folders need to get respectively and ran xxx.sh program.

Thanks in advance..

Satya

chrism01 07-22-2011 12:27 AM

Please show us your code so far and clarify your problem/required results

rsatyandra 07-22-2011 12:34 AM

res=`sqlplus -s system/system <<ENDOFSQL
spool abc.log
SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON TAB OFF
select HOS_VERSION from HOS_tab ;
exit
ENDOFSQL`
print $res " CURRENT VERSION"
x=7.5.3;
y=7.6;
z=7.6.1;
script_dir=/d03/CPM751NW/script;
echo "$res $x" | awk '{if ($1 >= $2) print $1 ":upgrading version" ; else prin
t $2 ": then run /$script_dir/753.sh"}'
echo "$res $y" | awk '{if ($1 >= $2) print $1 ":upgrading version"; else print
$2": then run /$script_dir/760.sh"}'
echo "$res $z" | awk '{if ($1 >= $2) print $1 ":upgraded version"; else print $
2 ": then run /$script_dir/761.sh"}'
print -u2 "OUTPUT ${?}"

Here $script_dir/xxx.sh file not excuting respectively ..

Thanks for your response..

satya

grail 07-22-2011 08:08 AM

Firstly, please place code in [code][/code] tags so it is readable.

Second, would you mind explaining further what you mean by:
Quote:

Here $script_dir/xxx.sh file not excuting respectively ..

AnanthaP 07-22-2011 08:25 PM

Shell variables, aren't automatically visible inside awk. You have to map them into awk using the -v switch in the command line.

Something like:
`awk -v cur=$rec -v dat=$x '{if ($cur >= $dat) print $1 ":upgrading version" ; else print $2 ": then run /$script_dir/753.sh"}'

and so on

Alternatively:
res=`sqlplus -s system/system <<ENDOFSQL
spool abc.log
SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON TAB OFF
select HOS_VERSION from HOS_tab ;
exit
ENDOFSQL`
print $res " CURRENT VERSION"
x=7.5.3;
y=7.6;
z=7.6.1;
script_dir=/d03/CPM751NW/script;
if [ $res -gt $x ]
then
print "Upgrading form $x to $res"
else
$script_dir/753.sh
fi
and so on for the other two tests.

OK

gnashley 07-23-2011 03:40 AM

print "Upgrading form $x to $res"
'print' is not a shell command. Use 'echo' instead.


$script_dir/753.sh
If the scripts don't have the executable bit set, the above won't run the script. You could use:
sh $script_dir/753.sh
or check/set the file executable before trying to run it.

konsolebox 07-23-2011 12:28 PM

you might as well have something similar to this:
Code:

#!/bin/bash

res=$(sqlplus -s system/system <<ENDOFSQL
spool abc.log
SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON TAB OFF
select HOS_VERSION from HOS_tab ;
exit
ENDOFSQL)

echo "$res CURRENT VERSION"

x=7.5.3
y=7.6
z=7.6.1;

script_dir=/d03/CPM751NW/script

if [[ ! $res < "$z" ]]; then
        ( . "/$script_dir/761.sh"; )
elif [[ ! $res < "$y" ]]; then
        ( . "/$script_dir/760.sh"; )
elif [[ ! $res < "$x" ]]; then
        ( . "/$script_dir/753.sh"; )
fi

print -u2 "OUTPUT ${?}"


grail 07-23-2011 02:29 PM

So I am curious that 2 people so far have suggested the following:
Code:

[[ $res > (or -gt) $x (or y or z)]]
As it will be a string comparison what happens when the version in $res is say 7.10.1. Whilst visually this is greater than say the value of z being 7.6.1
it will not return that way.

Just a thought.

konsolebox 07-23-2011 11:53 PM

@grail: yeah i guess it won't work with that. we have to compare each level of version number 1 by 1.

Code:

#!/bin/bash

res=$(sqlplus -s system/system <<ENDOFSQL
spool abc.log
SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON TAB OFF
select HOS_VERSION from HOS_tab ;
exit
ENDOFSQL)

echo "$res CURRENT VERSION"

x=7.5.3
y=7.6
z=7.6.1

script_dir=/d03/CPM751NW/script

IFS=. read R <<< "$res.0.0.0"

shopt -s extglob

VALID=false

for A in z y x; do
        IFS=. read V <<< "${!A}"
        for I in "${!V[@]}"; do
                if [[ ${R[I]} != +([[:digit:]]) ]] || [[ R[I] -lt V[I] ]]; then
                        ## [[ ${R[I]} != +([[:digit:]]) ]] has logic overhead but some factors are missing so I place it here; this means it's not valid for the current version that's being compared
                        continue 2
                elif [[ R[I] -gt V[I] ]]; then
                        VALID=true
                        break 2
                fi
        done
        VALID=true
        break
done

[[ $VALID = true ]] && IFS='' eval "( . \"/\$script_dir/${V[*]}.sh\"; )"

print -u2 "OUTPUT ${?}"

thanks for the tip by the way :)

grail 07-24-2011 07:42 AM

Quote:

we have to compare each level of version number 1 by 1.
Well not exactly :) Without worrying about the rest of the code, something like this could help this section:
Code:

if [[ $x != $(echo -e "$x\n$res" | sort -V | tail -n1) ]]
then
    echo now we know res is higher
fi

There are probably better ways but this was the first that occurred to me.

konsolebox 07-24-2011 09:15 AM

@grail: +1 :)


All times are GMT -5. The time now is 06:37 PM.