Home > Mobile >  How to run csh script in bash shell
How to run csh script in bash shell

Time:01-03

My default shell is bash in Ubuntu 14.04. I have a csh script file named clean.sh with the following make command:

#! /bin/csh -f
make -f commande.make del

And commande.make has

CKHOME=../CHEMKIN/DATA_BASES
LIN_DATA=${CKHOME}/LIN_FILES/
LINK_CKTP=${CKHOME}/LINK_CKTP_FILES/
#-----------------------------------------------------
include schema_cinetique.make
LINKFILE=${NAME}_LINK
LINKTPFILE=${NAME}_LINKTP
LINKFILE_OLD=${NAME_OLD}_LINK
LINKFILE_NEW=${NAME_NEW}_LINK
#-----------------------------------------------------
cplink :    
    ${COPY} ${LINK_CKTP}${LINKFILE} LINK
cplink2 :   
    ${COPY} ${LINK_CKTP}${LINKFILE} LINKZ1
tplink :    
    ${COPY} ${LINK_CKTP}${LINKTPFILE} LINKTPZ1
calcul :
    ${COPY} jobtimp1  LJOBNZ1
    ${COPY} unsteadyf.dat1 DATZ1
del :
    ${DELETE} LINKZ1 LINKTPZ1 LJOBNZ1 DATZ1 SOLASUZ1

I opened the terminal and moved to the location and tried

./clean.sh

or

csh clean.sh & 

or

csh -f clean.sh 

Nothing worked.

I got the following line in the terminal,

LINKZ1 LINKTPZ1 LJOBNZ1 DATZ1 SOLASUZ1
make: LINKZ1: Command not found
make: *** [del] Error 127

So, how to run clean.sh file ?

CodePudding user response:

You are confused. The Csh script contains a single command which actually runs identically in Bash.

#!/bin/bash
make -f commande.make del

Or, for that matter, the same with #!/bin/sh. Or, in this individual case, even sh clean.sh, since the shebang is then just a comment, and the commands in the file are available in sh just as well as in csh.

Once make runs, that is what parses and executes the commands in commande.make. make is not a "Fortran command", it is a utility for building projects (but the makefile named commande.make probably contains some instructions for how to compile Fortran code).

In the general case, Csh and Bash are incompatible, but the differences are in the shell's syntax itself (so, the syntax of loops and conditionals, etc, as well as variable assignments and various other shell builtins).

As an aside, Csh command files should probably not have a .sh extension, as that vaguely implies Bourne shell (sh) syntax. File extensions on Unix are just a hint to human readers, so not technically important; but please don't confuse them/us.

(As a further aside, nobody should be using Csh in 2022. There was a time when the C shell was attractive compared to its competition, but that was on the order of 40 years ago.)

The subsequent errors you are reporting seem to indicate that the makefile depends on some utilities which you have not installed. Figuring that out is a significant enough and separate enough question that you should probably ask a new question about that, probably with more debugging details. But in brief, it seems that make needs to be run with parameters to indicate what NAME and COPY (and probably some other variables) should be. Try with make -f commande.make COPY=cp DELETE=rm NAME=foobar for a start, but it's probably not yet anywhere near sufficient.

(I would actually assume that there will be a README file or similar to actually instruct you how to use commande.make since it seems to have some local conventions of its own.)

CodePudding user response:

It seems the script is written having portability in mind, i.e. the name of the cp and rm binaries is kept in variables rather than hard-coding it. My best guess is that this has been done to make it possible to run the script on non UNIX systems, like Windows.

To make it work, export the respective variables before running the script. For the del action you are calling, only the DELETE variable is needed. It should be set to rm which is the command used to remove files on Linux:

export DELETE=rm
./clean.sh

Note: exporting the variable can also be done in one line when invoking the script, by prepending it to the command line:

DELETE=rm ./clean.sh

This behaviour is described in the bash manual:

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

  • Related