-1

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."

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
A.Chandra
  • 1
  • 2

3 Answers3

2

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.

user1934428
  • 19,864
  • 7
  • 42
  • 87
0

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
Bayou
  • 3,293
  • 1
  • 9
  • 22
0

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.