Code optimization


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code optimization
# 8  
Old 06-14-2018
Yes. After trying and finding out myself, I noticed the subtle difference in R and N assignments between post#2 and #5 - the $ sign!
# 9  
Old 06-14-2018
Simply break the loop if ORACLE_SID got a value!
(You do not need to check the boundaries. Imagine you have 10+ items...)
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
  [[ -n $ORACLE_SID ]] && break
  printf "${R}Invalid number!${N}\n"
done

#^D pressed?
[[ -n $ORACLE_SID ]] || exit

export ORACLE_SID HOST=${ORACLE_SID}_qa
echo "HOST=$HOST"

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 10  
Old 06-14-2018
You might as well consider this adaption of your original code:

Code:
R=$'\033[0;31m'
N=$'\033[0m'
cat <<-EOF

        1 - db_a
        2 - db_b
        3 - db_c
        4 - db_g
        5 - db_o
        6 - db_e
        7 - db_k
        8 - db_t

${R}Choose database $N

        EOF

read NUM
export ORACLE_SID=db_$(printf "\\$(printf "%o" $((0140+NUM)))")
export HOST=${ORACLE_SID}_qa
echo $ORACLE_SID, $HOST

# 11  
Old 06-14-2018
Thank you all, finally I use MadeInGermany code modification.
# 12  
Old 06-15-2018
Hi primo102...

Just an observation and slightly off topic and assumes bash.
I have no idea which OS you are using but there are now various Linux flavours that use terminal __windows__ of varying sizes.
I am assuming that you are using the standard size of 80x24, however.....
I have seen 60x21 - (the smallest I have seen), 70x23, 77x25, 80x25 and a couple of others.
Some of the default terminal windows inside certain Linux flavours will not be able to be resized on the fly, the 60x21 one was just an example, so you might have to work around those limits!
You can check yours by using stty size and this will give the figures in reverse, something like 24 80 ...
For those terminal windows that CAN be resized on the fly you can use this at the start of your script after the "shebang":
Code:
printf "%b" "\x1B[8;24;80t"

If you really want to make it pretty some of these terminal windows can even be written into the title bar:
Code:
printf "%b" "\x1B]0;Your title bar text here.\x07"

And return back to default title:
Code:
printf "%b" "\x1B]0;\x07"

Again, purely just an observation as you are using a neatly ordered selection process.
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