LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2011, 07:05 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
Making sure script is running in bash!


is this a good way of doing it? Shall I use & when starting the new process? Itested in bash and dash.

testbash=$(ps -e | grep $$ | grep bash)
if [ 0 = ${#testbash} ]; then
echo "new process"
bash MYPROCESSNAME
exit 0
fi

echo "It's in bash jippi"
 
Old 02-15-2011, 07:09 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
I think it's better to specify it in the shebang

#!/bin/bash

Don't use

#!/bin/sh

when it's a bash script.

Last edited by Guttorm; 02-15-2011 at 07:11 AM.
 
Old 02-15-2011, 07:55 AM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
so if someone will call with sh, dash or so its up to them?

and i realize that i had to change mod for it to work.


thanks
 
Old 02-15-2011, 09:11 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by kalleanka View Post
so if someone will call with sh, dash or so its up to them?
Nope. The hashbang (or shebang) tells the OS what needs to be used to execute/interpret the script that follows.

For example, if a script starts with:
#!/bin/bash - all that follows is done by bash,
#!/usr/bin/perl - all that follows is done by perl,
#!/bin/dash - all that follows is done by dash.

There are a lot more examples, but I guess you get the picture.

Also have a look here for a more comprehensive explanation: Shebang (Unix)

Hope this helps.
 
Old 02-15-2011, 10:59 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
I think Kalleanka's code helps in a situation where a user is using something like ksh interactively and types "sh scriptname", instead of doing the chmod. I often do that because it's faster, but I use bash
 
Old 02-15-2011, 11:21 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
You could check that BASH is set:
Code:
if [ -z "$BASH" ]; then
   echo this script must be run with bash
   exit 1
fi
 
Old 02-15-2011, 11:26 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by ntubski View Post
You could check that BASH is set:
Code:
if [ -z "$BASH" ]; then
   echo this script must be run with bash
   exit 1
fi
You shouldn't rely on this method, if you switch shells from bash to say csh the BASH variable is still present although you are now using csh....

See posts #8 and #11

Last edited by druuna; 02-16-2011 at 02:10 AM. Reason: Mix up of $BASH and $SHELL
 
Old 02-15-2011, 06:46 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by druuna View Post
Hi,
You shouldn't rely on this method, if you switch shells from bash to say csh the BASH variable is still present although you are now using csh....
No, the variable is not exported:
Code:
~/tmp$ echo $BASH
/bin/bash
~/tmp$ zsh
% echo $BASH

% 
 
Old 02-16-2011, 12:51 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by ntubski View Post
No, the variable is not exported:
Code:
~/tmp$ echo $BASH
/bin/bash
~/tmp$ zsh
% echo $BASH
You are correct, I read BASH and was thinking SHELL variable.....

My mistake!
 
Old 02-16-2011, 01:50 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I wonder, is $BASH exportable to other shells? i.e. if bash runs ash.. will $BASH exist in the environment of ash?.. i forgot about this already.. but I think $BASH_VERSION or $BASH_VERSINFO is safe..

Last edited by konsolebox; 02-16-2011 at 01:55 AM.
 
Old 02-16-2011, 01:56 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by konsolebox View Post
I wonder, is $BASH exportable to other shells? i.e. bash runs ash.. will $BASH exist in the environment of ash?.. i forgot about this already.. but I think $BASH_VERSION or $BASH_VERSINFO is safe..
ntubski was correct about $BASH. $BASH_VERSION and $BASH_VERSINFO are also _not_ exported when switching shells.
 
Old 02-16-2011, 02:08 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
@druuna: thanks for checking

off topic: bash 4.2 has been released last feb. 13 and i've checked the changelog.. one of its features says that a new option in declare/typeset has been added and it's '-g' .. with that option, variables can now be declared global in any scope even inside functions.. finally. and i'm the one who made this request before as well

i can finally make the final versions of Shell Script Loader with this ^_^
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Making bash script WAIT after starting subshell mintybadger Programming 9 08-04-2011 04:49 AM
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
bash script: making options? johngreenwood Programming 4 01-04-2007 01:55 PM
making a bash script file Berticus Linux - General 1 10-15-2005 11:14 PM
making a bash script student04 Linux - Software 5 01-11-2005 03:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration