Csh - how to combine multiple commands in one line

Hey everyone,

I am working in an environment where the different users can use ksh or csh. My situation is that I need the same result with one single command line.
I am searching for the real path the file is in.

My ksh input and output

ts2:ts2adm> cd $(dirname $(which sapcontrol)); pwd -P
/sapmnt/TS2/exe/uc/sun_64

My csh input and output

k11:k11adm 1% cd $(dirname $(which sapcontrol)); pwd -P
Variable syntax

It seems that csh does not support such "fancy" things as combining multiple commands in one line.

I already tried the following calls

k11:k11adm 46% cd `dirname `which sapcontrol`` ; pwd -P
cd: Too many arguments
k11:k11adm 47% dirname `which sapcontrol`
/usr/sap/K11/SYS/exe/nuc/sun_64

with just two commadns combined it seems to be working.

Can anyone help?

---------- Post updated 05-02-16 at 06:59 AM ---------- Previous update was 04-02-16 at 12:24 PM ----------

I found a workaround for this. I am currently using

set  temp_wsc = `which sapcontrol`; set temp_wsc = `dirname $temp_wsc`; cd  $temp_wsc; pwd -P

This was the best I could whink of.
If anyone has an other idea, I am willing to try everything to make this a little smoother.

As you have seen, and as has been said many times before in this forum, csh is more intended for interactive use. And, shells based on Bourne shell syntax (such as ash , bash , dash , ksh , sh , and many others) are much easier to use for shell scripting.

Why not just use ksh instead of trying to convert working ksh scripts to csh ?

You do know that if you have a script starting with:

#!/bin/ksh

in an executable file, that file can be invoked by a csh script or by a ksh script and will run with ksh interpreting the commands inside that script. Thinking that you should write a script in the common set of tools available to both csh and ksh so that code can be executed by both csh and ksh scripts produces a lot of busywork that will created slower scripts with much more restricted capabilities than just writing your scripts using ksh .

Nested `commands` are problematic.
While ksh and bash allow the \ escape of the inner back-ticks, you have found the $(commands) that can be properly nested.
The csh does not even allow the \ escape of the inner back-ticks; nesting them is plain impossible. You have to use a variable for each step.

set temp_wsc=`which sapcontrol`; cd "`dirname $temp_wsc`"; unset temp_wsc; pwd -P

Once you have a variable, you can avoid the dirname by means of a variable modifier:

set temp_wsc=`which sapcontrol`; cd "$temp_wsc:h"; unset temp_wsc; pwd -P

Tip: you better have no space around the = in a variable assignment, and have command arguments in "quotes". This is even true for ksh and bash:

cd "$(dirname "$(which sapcontrol)")"; pwd -P
1 Like