aspose file tools*
The moose likes Linux / UNIX and the fly likes declaring the shell variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "declaring the shell variable" Watch "declaring the shell variable" New topic
Author

declaring the shell variable

Bhopal Singh
Greenhorn

Joined: Jul 21, 2005
Posts: 11
Hi All,

I want to write a shell script that can work with C-based [tcsh,csh etc] and non C-based [bash,ksh etc] shells.
I need to declare some shell variables in this script and different shells has different syntax to declare shell variables. So, my problem is how to declare my variable in script so that it can run in all the shells?

my test.sh is as follows
#!/bin/sh
abc=7
myname=bhopal
......
....

So, now the shell script can be invoked in following manners
a)
1. chmod +x test.sh
2. ./test.sh

or

b)
2. /bin/tcsh test.sh

In case a) the "sh" shell will be used to execute test.sh and it will run successfully, but if the b) method is used to run the script it will fail.

One of the solution I can think of is to test the shell used to invoke the script e.g.

if [invoke_shell_name contains either tcsh or csh] then
set abc=12
set myname=bhopal
else
abc=12
myname=bhopal
fi
...
...

So, my quesiton is can I determine the shell script used to execute my shell script on the fly?

I will appreciate any other approach to write a SHELL independent script.

Thanks and Regards,
~Bhopal
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

I just stick to bash, myself, but if you "source" a shell script and that script has an explicit shell specififer, I'm pretty sure you can avoid having to test:


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaring the shell variable