LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How we can use pointer in script (https://www.linuxquestions.org/questions/linux-newbie-8/how-we-can-use-pointer-in-script-4175430260/)

karan4141 10-03-2012 06:40 AM

How we can use pointer in script
 
Hi Team,

I have built script to start & stop SAP application. In this script i have given system SID & now my script is too big. I want to do some changes in this script so the single code can execute for multiple SIDs. For Ex- I have 20 systems like ECD,BWD,CXD...etc & right now i have built script for all SIDs now i just want to keep all SIDs & host name in one text file & create some varible like "SID" & so script can read thi value from text file & than execute my script
./stsrtsap <SID> .

My script

#!/bin/sh
#
# Start / stop SAP
Action=$1
Inst=$2
case "$Action" in
"start")

case "$Inst" in
PIN)

su - orapin -c "lsnrctl start LISTENER_PIN"
su - pinadm -c "startsap"
su - pinadm -c "sapccm4x -DCCMS pf=/usr/sap/sapmnt/PIN/profile/ERN_DVEBMGS50_vhscpinapp01"
su - daaadm -c "startsap"
;;

BWN)
su - orabwn -c "lsnrctl start LISTENER_BWN"
su - bwnadm -c "startsap"
su - bwnadm -c "sapccm4x -DCCMS pf=/usr/sap/sapmnt/BWN/profile/BWN_DVEBMGS60_vhscbwnapp01"
su - daaadm -c "startsap"

;;
*)
esac
;;


"stop")

PIN)
su - daaadm -c "stopsap"
su - pinadm -c "sapccm4x -stop pf=/usr/sap/sapmnt/PIN/profile/ERN_DVEBMGS50_vhscpinapp01"
su - pinadm -c "startsap"
su - orapin -c "lsnrctl stop LISTENER_PIN"

;;

BWN)
su - daaadm -c "stopsap"
su - bwnadm -c "sapccm4x -stop pf=/usr/sap/sapmnt/BWN/profile/BWN_DVEBMGS600_vhscbwnapp01"
su - bwnadm -c "stopsap"
su - orabwn -c "lsnrctl stop LISTENER_BWN"

;;
*)
esac
;;

*)

echo "Usage: $0 { start|stop }"

exit 1

;;

esac

exit 0

I am a SAP Basis guy & trying to learn linux shell scripting ,Please help me to built this script.

schneidz 10-03-2012 07:09 AM

the basic way to read a var from a file is like so
Code:

[schneidz@hyper clips]$ cat karan4141.txt
hello
world
l33t
h4x0rz
chun-li
akuma
[schneidz@hyper clips]$ cat karan4141.txt | while read line
> do
>  echo line = $line
> done
line = hello
line = world
line = l33t
line = h4x0rz
line = chun-li
line = akuma


karan4141 10-03-2012 08:15 AM

Thanks for reply !!

I want to crete file like below


$ cat ihost
PIN PINAPP001
BWN BWNAPP01
CXN CXNAPP01

Now when i execute my script "startsap.sh" this scpript can read SID from ihost.

./startsap PIN than this PIN replace <sid> in my script & than execute script for PIN.

case "$Inst" in
PIN)

su - ora<sid> -c "lsnrctl start LISTENER_<SID>"
su - pin<sid> -c "startsap"
su - pin<sid> -c "sapccm4x -DCCMS pf=/usr/sap/sapmnt/<SID>/profile/<SID>_DVEBMGS50_vhscapp01"
su - daaadm -c "startsap"

catkin 10-03-2012 08:44 AM

Modifying schneidz's technique slightly and extending it, startsap.sh could contain something like
Code:

while read sid something_else
do
    su - ora$sid -c "lsnrctl start LISTENER_$sid"
    <more commands using $sid and maybe $something_else>
done < ihost

That presumes you have logon names oraPIN etc. which would be valid but unconventional. In case the logon is orapin, use ora${sid,,}. The technique is shell parameter expansion, here used to translate upper case to lower case.

pan64 10-03-2012 09:05 AM

in case you want to convert a variable to upper or lower case, here is a list about the possibilities, see in the middle (in bash 4): http://stackoverflow.com/questions/2...hell-scripting

karan4141 10-04-2012 07:05 AM

Hi Catkin,

Thanks for reply !!

You informaion is very helpful. Should i add code provided by you like below

#!/bin/sh

#

# Start / stop SAP

Action=$1
Inst=$2

case "$Action" in

"start")

case "$Inst" in
SID)

while read sid something_else
do
su - ora{$sid..} -c "lsnrctl start LISTENER_$sid"
su - ${sid,,}adm -c "startsap"
<more commands using $sid and maybe $something_else>
done < ihost

cat ihost

PIN
CXN
ECH
.....etc

catkin 10-04-2012 08:01 AM

Please use CODE tags when posting code. It is most easily done by using Advanced editing and using the # button.

Assuming startsap.sh is called with arguments/parameters "start" and "SID" then it looks OK (subject to testing) as long as you replace "<more commands using $sid and maybe $something_else>" with what you need.

karan4141 10-04-2012 08:22 AM

Thanks for reply !!

I have done the change & try to execute but i am getting syntex error

######################################################################################
Code:

#!/bin/sh

# Start / stop SAP

typeset  sid
Action=$1
Inst=$2

case "$Action" in

"start")

        case "$Inst" in
        sid)
while read sid
do
su - ora{$sid..} -c "lsnrctl start LISTENER_$sid"
su - ${sid,,}adm -c "startsap"
done < ihosts


;;
*)
      esac
          ;;
esac
*)


        echo "Usage: $0 { start|stop }"

        exit 1

        ;;

esac

exit 0

#######################################################################

Below is the output of this script

[root@HSCDEV9 /tmp]# ./test start BWU
./test: line 27: syntax error near unexpected token `)'
./test: line 27: `*)'
[root@HSCDEV9 /tmp]#

pan64 10-04-2012 08:47 AM

remove esac on line 27 and 29

catkin 10-04-2012 09:14 AM

Indentation makes that sort of error obvious. For example
Code:

case "$Action" in
    "start")
        case "$Inst" in
            sid)
                while read sid
                do 
                  su - ora{$sid..} -c "lsnrctl start LISTENER_$sid"
                  su - ${sid,,}adm -c "startsap"
                done < ihosts
                ;; 
            *) 
        esac
        ;; 
    *) 
        echo "Usage: $0 { start|stop }"
        exit 1
        ;; 
esac

exit 0


karan4141 10-04-2012 09:17 AM

Thanks for reply !!

I have removed the entries but also getting the same error. Below is my full script & output

Code:

typeset  sid
Action=$1
Inst=$2

case "$Action" in

"start")

        case "$Inst" in
        sid)
while read sid
do
su - ora{$sid..} -c "lsnrctl start LISTENER_$sid"
su - ${sid,,}adm -c "startsap"
done < ihosts


;;
*)
      esac
          ;;
"stop")

      sid|SID)
      while read sid
do

        su - ${sid,,)adm -c "stopsap"
 done < ihosts
;;
*)esac
;;
*)

        echo "Usage: $0 { start|stop }"

        exit 1

        ;;
esac

exit 0

Code:

[root@HSCDEV9 /tmp]# ./test start BWD
./test: line 28: syntax error near unexpected token `)'
./test: line 28: `      sid|SID)  '

I have devloped the below script is working fine but the issue is i have to write code for each SID(Ex- PIN,BWN,CXN..etc). So i just want to devlop sigle code which can get SID from any text file & execute the below script .

Code:

#!/bin/sh

#

# Start / stop SAP

Action=$1
Inst=$2

case "$Action" in

"start")

        case "$Inst" in
        PIN)

            su - orapin -c "lsnrctl start LISTENER_PIN" 
            su - pinadm -c "startsap"
           
        ;;
*)
      esac
          ;;
"stop")

        PIN)
           
            su - pinadm -c "startsap"
            su - orapin -c "lsnrctl stop LISTENER_PIN"
;;
*)
      esac
          ;;

*)

        echo "Usage: $0 { start|stop }"

        exit 1

        ;;

esac

exit 0


catkin 10-04-2012 12:03 PM

Before the stop case there is a ;; after *)

In the stop case there is an unmatched brace in su - ${sid,,)adm -c "stopsap"

After that there is
Code:

*)esac
;;
*)

which is at least messy and probably wrong.

After the closing esac there is
Code:

        echo "Usage: $0 { start|stop }"
 
        exit 1
 
        ;;
esac

The ;; and esac are wrong.

Fixing all that and using indentation so such errors would be obvious
Code:

typeset  sid
Action=$1
Inst=$2

case "$Action" in
    "start")
        case "$Inst" in
            sid)
                while read sid
                do
                    su - ora{$sid..} -c "lsnrctl start LISTENER_$sid"
                    su - ${sid,,}adm -c "startsap"
                done < ihosts
                ;;
            *)
        esac
    "stop")
        sid|SID)
        while read sid
        do
            su - ${sid,,}adm -c "stopsap"
        done < ihosts
        ;;
    *)
        echo "Usage: $0 { start|stop }"
        exit 1
        ;;
esac

exit 0

For perfection, the typeset sid is not required (it has no effect) and the case statement does not require a ;; before esac (it's OK to hve it but not required)

karan4141 10-05-2012 04:07 AM

Hi Catkin,

I have done the same you have pasted here but i am still getting error

Code:

typeset  sid
Action=$1
Inst=$2

case "$Action" in
    "start")
        case "$Inst" in
            sid)
                while read sid
                do
                    su - ora${sid,,} -c "lsnrctl start LISTENER_$sid"
                    su - ${sid,,}adm -c "startsap"
                done < ihosts
                ;;
            *)
        esac
    "stop")
        sid|SID)
        while read sid
        do
            su - ${sid,,}adm -c "stopsap"
        done < ihosts
        ;;
    *)
        echo "Usage: $0 { start|stop }"
        exit 1
        ;;
esac

exit 0

Code:

[root@DEV9 tmp]# ./test start BWU
./test: line 22: syntax error near unexpected token `)'
./test: line 22: `    "stop")'
[root@HSCDEV9 tmp]#


schneidz 10-05-2012 07:01 AM

i am not very familiar with cases but for your embedded case, you dont define anything for *). maybe it needs something like ;;.

karan4141 10-09-2012 05:41 AM

Sorry !! for trhe late reply but i am getting the same error after removing *) from inner case statment.

Code:

typeset  sid
Action=$1
Inst=$2

case "$Action" in
    "start")
        case "$Inst" in
            sid)
                while read sid
                do
                    su - ora${sid,,} -c "lsnrctl start LISTENER_$sid"
                    su - ${sid,,}adm -c "startsap"
                done < ihosts
                ;;

        esac
    "stop")
        sid|SID)
        while read sid
        do
            su - ${sid,,}adm -c "stopsap"
        done < ihosts
        ;;
    *)
        echo "Usage: $0 { start|stop }"
        exit 1
        ;;
esac

exit 0
[root@HSCDEV9 tmp]# ./test start BWU
./test: line 22: syntax error near unexpected token `)'
./test: line 22: `    "stop")'
[root@HSCDEV9 tmp]#



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