Select command


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Select command
# 1  
Old 11-07-2017
Select command

Hi
I'm using the "select" command in the global_env.sh to log in to the application directory. This file is called in .bashrc profile.

Sample code:
Filename: global_env.sh


Code:
set -o vi

export severname=$(uname -n)
printf '%s\n%30s\n%s\n' "***********************" "Welcome to $severname" "***********************"

export PS1='$USER:$PWD==>

select env in $(ls -ltrh /app/dwh/|grep ^d|awk '{print $9}') Exit
do
   case "$env" in
        Exit)
             exit
             ;;
        *)
           echo $env
           cd \/app/dwh\/$env
           break
           ;;
   esac
done

Output:
Code:
1. Dir1
2. Dir2
3. Dir3
4. Dir4
5. Exit

This works fine. The moment I enter my credentials, the above options are displayed and successfully able to login to the selected directory.

When I reference the file /app/env/global_env.sh in a shell script, it prompts me to select the application directory. I tried like below but still not working.
When referenced the global_env.sh in a shell script it should not prompt the user to input the option.

Appreciate any suggestions on this.

ScriptName: /var/dwh/Dir1/Test.sh
Code:
#!/bin/bash
export $env=Dir1
. /usr/env/global_env.sh

rm -f /app/dwh/Dir1/*.log

exit 0;

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments (as required by forum rules).

Last edited by Don Cragun; 11-07-2017 at 03:43 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 11-07-2017
Not clear. WHAT is "not working"?

Except for setting a shell option and the PS1 variable, and printing a welcome message, the only thing the script does is selecting the environment working directory - which you want to suppress if run interactively? Please explain the underlying logics.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-07-2017
Thanks for the response RudiC.
In the global environment file, we have all DB credentials, database client path, schema names and all common functions. This profile is set to login profile for User A.
But user B wants to access the /usr/env/global_env.sh in his Unix script, he will just reference it in the shell (. /usr/env/global_env.sh) but this will prompt for the directory selection, I want to suppress the directory prompt when using the profile in shell script by assigning the directory name which I want to log in, before referencing the environment file in the shell script.
# 4  
Old 11-07-2017
Still not clear. Why should B source that script if s/he doesn't need the credentials, client path, etc.? Howsoever, wildly guessing based on the little info you gave so far and from the code you show in post#1, do

- export env=Dir1 (eliminate the $ ) in your "shell script"
- [ $env ] && select env in $(ls -l | awk '/^d/ {print $9}' . . . in global_env.sh

to circumnavigate the working directory selection but execute the rest of the script. As you can see, the -rth option to ls is unnecessary, as is the grep in the pipe.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-07-2017
Quote:
Originally Posted by cheers799
Thanks for the response RudiC.
In the global environment file, we have all DB credentials, database client path, schema names and all common functions. This profile is set to login profile for User A.
But user B wants to access the /usr/env/global_env.sh in his Unix script, he will just reference it in the shell (. /usr/env/global_env.sh) but this will prompt for the directory selection, I want to suppress the directory prompt when using the profile in shell script by assigning the directory name which I want to log in, before referencing the environment file in the shell script.
So, (1) you are showing the tail end of /usr/env/global_env.sh; (2) User A sources it from his/her profile; (3) User B sources it from their shell script. Is that correct? And when called from the shell script you don't want the select to be executed?

Bash has a special variable $- which will contain the character i if it is interactive. You could test for this with say
Code:
if [[ "$-" == *i* ]]
then
# select code here
fi

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 6  
Old 11-07-2017
Be careful - the script is sourced, not executed as of post#3. Difference:

Code:
./shscr
echo $-
hvxB
. ./shscr
echo $-
himvxBHs

# 7  
Old 11-07-2017
Thanks for the responses based on that, I used like below and it works fine
I followed the below logic
if [ -z $env ]; then

call select command

else
cd to $env directory.
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SQLPLUS command with more than 1 select statement

Hi all, I'm using below code processId=`sqlplus -s ${sysuser}/${syspwd} <<CHK_PROCESS whenever sqlerror exit sql.sqlcode; set head off feedback off echo off pages 0 SELECT PROCESS_ID FROM LSHADMIN.DATA_DOMAIN WHERE DOMAIN_NAME = '${tabname}' ... (8 Replies)
Discussion started by: Pratiksha Mehra
8 Replies

2. Shell Programming and Scripting

Select command help with blank input value

I have a select menu driven script using a case statment and cannot control what happens after a user's input is just <ENTER> or the <SPACEBAR>+<ENTER>. I want it to just hit the "MAIN" function and not redraw the options. I've look everywhere for the answer and am at a loss. Here's the code:... (4 Replies)
Discussion started by: ambroze
4 Replies

3. UNIX for Dummies Questions & Answers

Select Command - return

Hello everyone, A simple question which may have a suggested solution: I am using, and loving, the select command in a ksh93 script on AIX 6.1 to present users with menus. I have been successful in controlling all of key input by the users, I still have an issue with the RETURN key. When the... (4 Replies)
Discussion started by: gio001
4 Replies

4. Shell Programming and Scripting

How to use select into command in shell script?

I need to get total number of rows in a table by using select count(*) from table and need to assign into a variable in shell script. I used the following script, it does return the number of rows but also with oracle headers, Please help me to eliminate this headers. Shell Script #!/bin/sh... (16 Replies)
Discussion started by: vel4ever
16 Replies

5. Solaris

How to select last 7 days entry from SULOG or LAST command

Hi All, I need to get last 7 days log entries from sulog. The same has to be done for the last command. for ex: when i search for a user "abc" in sulog, i only want to check his entries for the last 1 week. The same has to be done for last command. Can anyone suggest some tips. ... (0 Replies)
Discussion started by: navdeepan
0 Replies

6. Shell Programming and Scripting

Error using select menu command

Hi All, I am trying to use the select command & the menu. below mention is my script #!/bin/bash 2 3 PS3="Is today your birthday? " #PS3 system variable 4 5 echo "\n" 6 7 8 select menu_selection in YES NO QUIT 9 do 10 11 ... (1 Reply)
Discussion started by: milindb
1 Replies

7. Shell Programming and Scripting

Select command to build menu

Hello everyone. I am using the select command to build a menu, here is my question: Is it possible to generate a menu which contains several sections and have a separator between the sections without having a selection number generated in front of the separator? This is a sample of what I would... (1 Reply)
Discussion started by: gio001
1 Replies

8. Shell Programming and Scripting

Select command array

Hello everyone, I have a quick question about the select command in ksh. I am using it to define a menu for user selection, I have found that the REPLY var contains the input from the user, I was interested to find out if there is a system variable that contains the whole menu as an array, the... (2 Replies)
Discussion started by: gio001
2 Replies

9. UNIX for Dummies Questions & Answers

How to select a particular media from the printer with a UNIX command

Problem Overview: We have a scheduler that prints report on any of the network printer. Problem Statement: We need to find a UNIX command that picks up either A4, legal or letter size paper form the printer. I found out a command but it's not working on our environment. ... (3 Replies)
Discussion started by: HelpMeOUt
3 Replies

10. UNIX for Dummies Questions & Answers

Command to select files with different extensions

I want to select files which have different extensions such as .cpp, .cs, .h I can select one of them as find . -name "*.cpp" but I want to select all of them in one command only. It should be pretty simple but I'm not able to get it. Any help with the command will be greatly appreciated. (1 Reply)
Discussion started by: MobileUser
1 Replies
Login or Register to Ask a Question