Guidance required in .profile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Guidance required in .profile
# 1  
Old 07-29-2019
Guidance required in .profile

Not every server I use has bash so I wanted to add an option to check if bash exists. This is what I tried. This is the error message it gives "-bash: [: missing `]'". It works before I add the and option.

Code:
if [ -s ~/.bashrc && -s /usr/bin/bash ]; then
    echo hi
    source ~/.bashrc;
fi


Last edited by RavinderSingh13; 07-30-2019 at 01:16 AM..
# 2  
Old 07-29-2019
The test ( or [ ) command does not like the && operator. Use (deprecated) -a instead, or use [[ ... ]].
# 3  
Old 07-29-2019
Quote:
Originally Posted by RudiC
The test ( or [ ) command does not like the && operator. Use (deprecated) -a instead, or use [[ ... ]].
Thank you. Worked perfectly.
# 4  
Old 07-29-2019
One could also portably use:
Code:
if [ -s ~/.bashrc ] && [ -s /usr/bin/bash ]; then

Although && isn't a test (or [ ... ]) utility operator, it is a shell operator.
# 5  
Old 07-30-2019
Quote:
Originally Posted by cokedude
Not every server I use has bash so I wanted to add an option to check if bash exists. This is what I tried. This is the error message it gives "-bash: [: missing `]'". It works before I add the and option.

Code:
if [ -s ~/.bashrc && -s /usr/bin/bash ]; then
    echo hi
    source ~/.bashrc;
fi

The problem with the above is, if bash exists but you are not running bash, your shell will baulk at an instruction in the .bashrc file.

Try something like this at the end of your .profile:
Code:
if [ -z "$BASH_VERSION" ] 
then
   if [ -x /usr/bin/bash ]
   then
      SHELL=/usr/bin/bash
      export SHELL
      exec $SHELL
   elif [ -x /bin/bash ]
   then
      SHELL=/bin/bash
      export SHELL
      exec $SHELL
   fi
fi

This way, if bash exists, but is not your login shell, your shell will switch to bash at the end of the .profile. You can also add a test for bash being in /usr/local/bin.

Andrew
# 6  
Old 07-30-2019
Quote:
Originally Posted by apmcd47
The problem with the above is, if bash exists but you are not running bash, your shell will baulk at an instruction in the .bashrc file.

Try something like this at the end of your .profile:
Code:
if [ -z "$BASH_VERSION" ] 
then
   if [ -x /usr/bin/bash ]
   then
      SHELL=/usr/bin/bash
      export SHELL
      exec $SHELL
   elif [ -x /bin/bash ]
   then
      SHELL=/bin/bash
      export SHELL
      exec $SHELL
   fi
fi

This way, if bash exists, but is not your login shell, your shell will switch to bash at the end of the .profile. You can also add a test for bash being in /usr/local/bin.

Andrew
Bash is thankfully my default shell. I was trying to solve the problem of when bash does not exist. Ksh is the system default but I changed my personal default to bash. How would I fall back to ksh if bash does not exist?

Quote:
Originally Posted by Don Cragun
One could also portably use:
Code:
if [ -s ~/.bashrc ] && [ -s /usr/bin/bash ]; then

Although && isn't a test (or [ ... ]) utility operator, it is a shell operator.
Do older systems not like this? Is that what you were saying with portably?

Code:
if [[ -s ~/.bashrc && /usr/bin/bash ]]; then
    #echo hi
    source ~/.bashrc;
fi

# 7  
Old 07-31-2019
Quote:
Originally Posted by cokedude
Bash is thankfully my default shell. I was trying to solve the problem of when bash does not exist. Ksh is the system default but I changed my personal default to bash. How would I fall back to ksh if bash does not exist?
So basically you are asking, "is my login shell bash or ksh?" And if there is no bash on your system it will be ksh, because that is the default, else you will have changed it by the time your regular .profile has been installed. Your SHELL variable will be set to your login shell and bash, from version 2 at least, will have the BASH_VERSION variable. So, either:
Code:
[ -n "$BASH_VERSION" ] && . ./.bashrc

or
Code:
case "$SHELL" in
*bash) . ./.bashrc ;;
*ksh) . ./.kshrc ;;
esac

should do it.
Quote:
Do older systems not like this? Is that what you were saying with portably?

Code:
if [[ -s ~/.bashrc && /usr/bin/bash ]]; then
    #echo hi
    source ~/.bashrc;
fi

POSIX shell and older shells don't support the double square bracket operator; and the single square bracket operator doesn't support the && and || operators. Personally, I think it's more readable to keep one expression per square bracket operator, whether it is single or double.

Andrew
This User Gave Thanks to apmcd47 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

New to AIX. Need Guidance

Hi There, I am new to AIX. I am eager to learn the AIX System administration or if there is any other prerequisite before this. Please can anyone help me or guide how to start with AIX, what all courses and certifications do I need to do. I have basic knowledge of UNIX. Please guide as I am... (3 Replies)
Discussion started by: rahulat302
3 Replies

2. Shell Programming and Scripting

Need Some guidance on scripting

Hey All, I am newbie on scripting and need some guidance from all the experts here. I am working on one project where I will check the status/health of around ten (10) solaris 10 servers. I have one central server from where I have already setup the passwordless SFTP and setup the cron... (1 Reply)
Discussion started by: supercops
1 Replies

3. UNIX for Dummies Questions & Answers

Just need some guidance on this nawk

Hi, I am trying to debug some KSH script and it has the nawk portion below. I just want to confirm whether I understand what it does correctly. Example usage of the nawk line is run as below: nawk '/^#/ {next} $1~/^'testp.cfg'$|^'testp.cfg'\.testdb\.com\.ph$/ { c=0 while... (1 Reply)
Discussion started by: newbie_01
1 Replies

4. AIX

Need guidance on VMStat

I need some guidance on the differences in observations, not sure how significantly different are they. Also, It would be nice to hear on the values and what the obvious tuning for performance missing. Observation 1 ending vmstat -v 3948544 memory pages ending vmstat -v ... (1 Reply)
Discussion started by: Snipper
1 Replies

5. Shell Programming and Scripting

guidance required: feed load search & display in shell scripting

Hi All, I am fairly new to Shell Scripting, however learning fast ;-) I have been asked to do the below basic shell script :confused: There are few feed files we are recieving in the server from multiple locations spread out during the day, rite now we are checking manually each file... (2 Replies)
Discussion started by: sachaan
2 Replies

6. Infrastructure Monitoring

trap in etc/profile and user .profile

Hello I really wonder what's trap in etc/profile and in each user .profile. I try to google for it but I think I have no luck. Mostly hit is SNMP traps which I think it is not the same thing. I want to know ... 1. What's a "trap 2 3" means and are there any other value I can set... (4 Replies)
Discussion started by: Smith
4 Replies

7. Shell Programming and Scripting

A little guidance needed :)

Hi guys, I'm learning the ropes of BASH and am doing a few exercises to see if its sinking in but have gotten stuck on something I KNOW is looking at me right in the face but just isn't registering. I'm creating a script that needs to get specific strings from a line. So using the "ls -l... (9 Replies)
Discussion started by: shadow0001
9 Replies

8. What is on Your Mind?

Career Guidance

Hi, I am a newbie to Unix, I was introduced to UNIX 8 months back during my Training, I was attracted to Unix as they give complete freedom. I would like to ask how can a OS Admin can go into development field of Unix. Currently I am working in a MNC in Backup- Storage Admin Domain I am... (1 Reply)
Discussion started by: sufi_431
1 Replies

9. AIX

NIM Guidance

I've just started to explore NIM and I'm looking for additional information on how to set it up and configure it. I've read through the "NIM A-Z" and have many unanswered questions. One question is how can I have the NIM server pull a mksysb of the clients and can I schedule this to happen... (1 Reply)
Discussion started by: scottsl
1 Replies

10. UNIX for Dummies Questions & Answers

changed .profile but didnt ./.profile, yet reflected changes

hi , i added ls -F to .profile. and i need to do ./.profile for the effect to take effect BUT i didnt and YET the next day when i came to work and log in, the changes took effect. i am on aix. please explain.. thanks (4 Replies)
Discussion started by: yls177
4 Replies
Login or Register to Ask a Question