A.Chandra
A.Chandra

Reputation: 1

How can I invoke both BASH and CSH shells in a same script

In the same script, I want to use some CSH commands and some BASH commands. Invoking one after the other giving me problems despite I am following the different syntax for respective shells. I want to know where is mistake in my code Your suggestions are appreciated!!

I am a beginner to shell, especially so for CSH. but the code I got has been written in CSH entirely. Since I have some familiarity with CSH, I wanted to tweak the existing CSH code by including BASH commands, which I am comfortable using it. When I tried BASH commands after CSH by invoking !#/bin/bash, it is giving some errors. I want to know if I am missing any options!!

#!/bin/csh
----
----
----
#!/bin/bash
dir2in="/nethome/achandra/NCEI/CCSM4_Historical/Forecasts"
filin2 ="ccsm4_0_cfsrr_Fcst.${ENS}.cam2.h1.${yyear[${iimonth}]}-${mmon[${iimonth}]}-${ssday}-00000.nc"
cp $dirin/$filin /nethome/achandra/NCEI/CCSM4_Historical_Forecasts/
ln -s /nethome/achandra/NCEI/CCSM4_Historical/Forecasts/$filin /nethome/achandra/NCEI/CCSM4_Historical_Forecasts/"${$filin%.nc.cdo}.nc"
#!/bin/csh 

I am getting errors such as "dirin: Undefined variable."

Upvotes: -1

Views: 1254

Answers (4)

Jon Labani
Jon Labani

Reputation: 11

You can. Here's a simple C-shell example:

#!/bin/csh

./test.sh

In this case, test.sh is a bash script. This will run the exact same way as it would if I were running "test.sh" standalone.

Upvotes: 1

Kurt Glaesemann
Kurt Glaesemann

Reputation: 91

If you want to be able to "source" a file and have it work in both, you can create a generic wrapper:

echo $shell | grep -q csh && goto CSH

# sh like
. /the/real/script.sh
return

# csh like
CSH:
source /the/real/script.csh

This does require the full path to the "real" scripts. To dynamically get the paths you can use ${BASH_SOURCE} in bash, ${0:a:h} in zsh, and $_ in csh. I do not know of any way to get that in sh or ksh.

Upvotes: 0

user1934428
user1934428

Reputation: 22225

You are asking here for "embedding one language into another", which, as @Bayou already explained, is not supported directly. Maybe you were spoiled from the HTML-world, where you can squeeze CSS and Javascript in between and maybe use some server side PHP or Ruby stuff too.

The closest to this are HERE-documents. If you write inside your bash script a

csh <<CSH_END
your ...
csh ....
commands ...
go here ...
CSH_END

these commands are executed in a child process driven by csh. It works the other way around with bash in the same way. Make sure that the terminator symbol (CSH_END in my example) starts in column 1.

Whether this will work for your application, I can't say, because things which run in the same process in your original script, now run in different processes.

Upvotes: 2

Bayou
Bayou

Reputation: 3441

You can't mix them up like you're suggesting. It's like asking "can I use PHP code in a Python script". However, most of the shells have options to run commands (-c), just as csh does. For using Bash within a sh script:

#! /bin/sh

CONDITION=$(/bin/bash -c "[[ 1 > 2 ]] || echo no")
echo $CONDITION
exit 0

Otherwise you could create separate files and execute them.

#! /bin/sh

CONDITION=$(./bash-script.sh)
echo $CONDITION
exit 0

You, of course, should use csh instead of sh. Both of my scripts will output the following text.

$ ./test.sh 
no

Upvotes: 0

Related Questions