Code optimization


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code optimization
# 1  
Old 06-14-2018
Code optimization

Hi all

I wrote below code:

Code:
#!/bin/sh

R='\033[0;31m'
N='\033[0m'

echo " ";
echo -e "${R}Choose database $N"
echo " ";
echo "1 - db_a"
echo "2 - db_b"
echo "3 - db_c"
echo "4 - db_g"
echo "5 - db_o"
echo "6 - db_e"
echo "7 - db_k"
echo "8 - db_t"
echo " "


read NUM

case $NUM in
       1)
                export ORACLE_SID=db_a
                export HOST=db_a_qa
                 ;;
       2)
		export ORACLE_SID=db_b
                export HOST=db_b_qa
                ;;
       3)
		export ORACLE_SID=db_c
                export HOST=db_c_qa
                ;;
       4)
		export ORACLE_SID=db_g
                export HOST=db_g_qa
                ;;
       5)
		export ORACLE_SID=db_o
                export HOST=db_o_qa
                 ;;
       6)
		export ORACLE_SID=db_e
                export HOST=db_e_qa
                ;;
       7)
		export ORACLE_SID=db_k
                export HOST=db_k_qa
                ;;
       8)
		export ORACLE_SID=db_t
                export HOST=db_t_qa
                ;;

       *) echo -e  "${R}Invalid number!${N}" 
				;;
esac
echo " "

do you have any idea how to optimize my code ? (to make it shorter eg.)
# 2  
Old 06-14-2018
Like so?
Code:
#!/bin/sh

R='\033[0;31m'
N='\033[0m'

OPTIONS="1 - db_a
2 - db_b
3 - db_c
4 - db_g
5 - db_o
6 - db_e
7 - db_k
8 - db_t"
set -- $OPTIONS

printf "${R}Choose database ${N}\n\n%s\n\n" "$OPTIONS"

read NUM
case $NUM in 
   [1-8]) 
                eval export ORACLE_SID=\$\{$((3*NUM))\}
                export HOST=${ORACLE_SID}_qa
                ;;

       *)       printf "${R}Invalid number!${N}\n"
                ;;
esac
echo

Or can it be bash as well?
# 3  
Old 06-14-2018
Sorry my mistake, script should be only bash.

Code:
#!/bin/bash

# 4  
Old 06-14-2018
With bash you have the select statement. Try
Code:
OPTIONS="db_a db_b db_c db_g db_o db_e db_k db_t"
PS3="Choose database: "
select ORACLE_SID in $OPTIONS; do echo $ORACLE_SID; HOST=${ORACLE_SID}_qa; echo $HOST; done
2) db_b
3) db_c
4) db_g
5) db_o
6) db_e
7) db_k
8) db_t
Choose database: 4
db_g
db_g_qa
Choose database: 8
db_t
db_t_qa
Choose database:

Press <Ctrl-D> to quit...

Last edited by RudiC; 06-14-2018 at 12:02 PM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-14-2018
To add your color options and answer checking and menu finishing, try something like this:

Code:
#!/bin/bash
R=$'\e[0;31m'
N=$'\e[0m'

OPTIONS=( db_a db_b db_c db_g db_o db_e db_k db_t )
PS3=$'\n'"${R}Choose database ${N}"

select ORACLE_SID in "${OPTIONS[@]}"; do
  if [[ $REPLY == [1-8] ]]; then
    export ORACLE_SID
    export HOST=${ORACLE_SID}_qa
    break
  fi
  printf "${R}Invalid number!${N}\n"
done 
echo

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 06-14-2018
With my system, above PS3 assignment doesn't give satisfying results. man bash:

Quote:
PS3 The value of this parameter is used as the prompt for the select command (see SHELL GRAMMAR above).
Note - it is NOT expanded!



You may want to try this circumnavigation:

Code:
PS3=$(printf $'\n'"${R}Choose database ${N}")


Last edited by RudiC; 06-14-2018 at 12:12 PM..
# 7  
Old 06-14-2018
Quote:
Originally Posted by RudiC
With my system, above PS3 assignment doesn't give satisfying results. man bash:


Note - it is NOT expanded!



You may want to try this circumnavigation:

Code:
PS3=$(printf $'\n'"${R}Choose database ${N}")

Note that I did not use expansions. Those should be literal characters..

Compare:
Code:
$ R=$'\e[0;31m'; N=$'\e[0m'; var=$'\n'"${R}Choose database ${N}" dash -c 'echo "$var"'

Choose database


Last edited by Scrutinizer; 06-14-2018 at 12:47 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CPU optimization

hi guys , I have 10 scripts suppose 1.sh , 2.sh ,3.sh ,4.sh ......10.sh each takes some time ( for instance 2 minutes to 40 minutes ) my server can run around 3-4 files at a time suppose, 1.sh , 2.sh , 3.sh are running currently now as soon as ANY ONE of the gets finished i... (4 Replies)
Discussion started by: Gl@)!aTor
4 Replies

2. Programming

Make file optimization

Hi , I need some help on optmizing the make file. I have the below scenario. SOUTHERN_XITEM_PROGS=\ sfmxitem.rel \ sfqxitem.rel SOUTHERN_XITEM_DEPS= southern_xitem.c SUPERSVU_XITEM_PROGS=\ su3xitem.rel SUPERSVU_XITEM_DEPS= supersvu_xitem.c $(SOUTHERN_XITEM_PROGS) : $$@.o... (1 Reply)
Discussion started by: angelinrajeesha
1 Replies

3. Shell Programming and Scripting

sed optimization

I have a process using the following series of sed commands that works pretty well. sed -e 1,1d $file |sed 1i\\"EHLO Broadridge.com" |sed 2i\\"MAIL FROM:${eaddr}"|sed 3i\\"RCPT TO:${eaddr}"|sed 4i\\"DATA"|sed 5s/.FROM/FROM:/|sed 6s/.TO/TO:/|sed 7,7d|sed s/.ENDDATA/./|sed s/.ENDARRAY// >temp/$file... (1 Reply)
Discussion started by: njaiswal
1 Replies

4. Shell Programming and Scripting

AWK optimization

Hello, Do you have any tips on how to optimize the AWK that gets the lines in the log between these XML tags? se2|6|<ns1:accountInfoRequest xmlns:ns1="http://www.123.com/123/ se2|6|etc2"> .... <some other tags> se2|6|</ns1:acc se2|6|ountInfoRequest> The AWK I'm using to get this... (2 Replies)
Discussion started by: majormark
2 Replies

5. UNIX for Dummies Questions & Answers

unix script optimization

I have a file which contains 9,200,000. It contains 125 clolumns. I have to rearrange some columns and exclude some of them. I scripted the following script to do the same. It is working fine but it is taking more than 4hrs to do it. can it be optmized. Here is the script LOC="/sourcefile/"... (3 Replies)
Discussion started by: max_payne1234
3 Replies

6. Shell Programming and Scripting

script optimization

:o Hi, I am writing a script in which at some time, I need to get the process id of a special process and kill it... I am getting the PID as follows... ps -ef | grep $PKMS/scripts | grep -v grep | awk '{print $2 }'can we optimize it more further since my script already doing lot of other... (3 Replies)
Discussion started by: vivek.gkp
3 Replies

7. Programming

compilation parameters, code optimization

Hi all, I implemented an application, through using c++ and compiled it with g++. At first, what I did is (@ compilation): g++ calcBacon.C -o test -DDEBUG after I ran my application it took almost 120 sec. to finish its execution when I compiled with optimization parameters, execution... (5 Replies)
Discussion started by: SaTYR
5 Replies

8. UNIX for Dummies Questions & Answers

Help on optimization of the script

Hi, I have prepared script which is taking more time to process. find below script and help me with fast optimized script:- cat name.txt | while read line do name=$(echo $line| awk '{print $8}') MatchRecord=$(grep $name abc.txt | grep -v grep ) echo "$line | $MatchRecord" | awk... (2 Replies)
Discussion started by: aju_kup
2 Replies
Login or Register to Ask a Question