Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Code optimization

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 1 Week Ago   -   Original Discussion by primo102
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 4:36 AM EDT
Location: Aachen, Germany
Posts: 12,916
Thanks: 429
Thanked 3,966 Times in 3,647 Posts
Yes. After trying and finding out myself, I noticed the subtle difference in R and N assignments between post#2 and #5 - the $ sign!
Sponsored Links
    #9  
Old Unix and Linux 1 Week Ago   -   Original Discussion by primo102
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 June 2018, 5:13 AM EDT
Location: Simplicity
Posts: 4,124
Thanks: 359
Thanked 1,402 Times in 1,259 Posts
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"

The Following 2 Users Say Thank You to MadeInGermany For This Useful Post:
primo102 (1 Week Ago), Scrutinizer (1 Week Ago)
Sponsored Links
    #10  
Old Unix and Linux 1 Week Ago   -   Original Discussion by primo102
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 June 2018, 4:36 AM EDT
Location: Aachen, Germany
Posts: 12,916
Thanks: 429
Thanked 3,966 Times in 3,647 Posts
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 Unix and Linux 1 Week Ago   -   Original Discussion by primo102
primo102's Unix or Linux Image
primo102 primo102 is offline
Registered User
 
Join Date: Nov 2014
Last Activity: 14 June 2018, 6:03 PM EDT
Location: Warsaw
Posts: 26
Thanks: 7
Thanked 0 Times in 0 Posts
Thank you all, finally I use MadeInGermany code modification.
Sponsored Links
    #12  
Old Unix and Linux 6 Days Ago   -   Original Discussion by primo102
wisecracker's Unix or Linux Image
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 June 2018, 3:08 AM EDT
Location: Loughborough
Posts: 1,268
Thanks: 379
Thanked 344 Times in 269 Posts
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.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed optimization njaiswal Shell Programming and Scripting 1 07-16-2009 03:25 AM
AWK optimization majormark Shell Programming and Scripting 2 06-02-2009 11:10 AM
script optimization vivek.gkp Shell Programming and Scripting 3 11-06-2008 04:37 AM
compilation parameters, code optimization SaTYR Programming 5 09-04-2008 12:07 AM
Help on optimization of the script aju_kup UNIX for Dummies Questions & Answers 2 11-20-2007 01:21 AM



All times are GMT -4. The time now is 05:32 AM.