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

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:

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

#!/bin/bash
export $env=Dir1
. /usr/env/global_env.sh

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

exit 0;

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.

1 Like

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.

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.

1 Like

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

if [[ "$-" == *i* ]]
then
# select code here
fi

Andrew

1 Like

Be careful - the script is sourced, not executed as of post#3. Difference:

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

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