Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declaring the shell variable

 
Bhopal Singh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18157
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic