• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

declaring the shell variable

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic