Csh - how to combine multiple commands in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Csh - how to combine multiple commands in one line
# 1  
Old 02-05-2016
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
Code:
ts2:ts2adm> cd $(dirname $(which sapcontrol)); pwd -P
/sapmnt/TS2/exe/uc/sun_64

My csh input and output
Code:
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
Code:
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
Code:
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.
# 2  
Old 02-05-2016
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:
Code:
#!/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.

Last edited by Don Cragun; 02-05-2016 at 02:49 AM.. Reason: Add note.
# 3  
Old 02-05-2016
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.
Code:
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:
Code:
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:
Code:
cd "$(dirname "$(which sapcontrol)")"; pwd -P


Last edited by MadeInGermany; 02-05-2016 at 04:54 PM.. Reason: consequent quoting
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine 2 Commands

Hello, I have the following code. I wonder if it can be combined into 1 command. y=`ls -1| tail -n 1` m=${y%.abc} Thank you. (3 Replies)
Discussion started by: april
3 Replies

2. Shell Programming and Scripting

Combine multiple commands

I have the following sh-script: konsole -T todo -e vi todo.txt & konsole -T window1 -e ssh user@server & konsole -T window2 -e ssh user@server2 -e cd directory & The first two lines are working fine. The first opens a txt-file, the second opens a ssh-connection. The third line... (6 Replies)
Discussion started by: andre666
6 Replies

3. Shell Programming and Scripting

Combine multiple awk commands

Hi Team, I am getting input like below $ ps -ef | grep pmon | grep -v asm | grep -v grep oracle 3246 1 0 00:03 ? 00:00:01 ora_pmon_racora1 oracle 4367 1 0 00:03 ? 00:00:01 ora_pmon_test1 oracle 6893 1 0 00:03 ? 00:00:01 ora_pmon_gipora1... (6 Replies)
Discussion started by: kamauv234
6 Replies

4. Shell Programming and Scripting

Combine multiple lines into single line

Hi All , I have a file with below data # User@Host: xyz @ # Query_time: t1 Lock_time: t2 Rows_sent: n1 Rows_examined: n2 SET timestamp=1396852200; select count(1) from table; # Time: 140406 23:30:01 # User@Host: abc @ # Query_time: t1 Lock_time: t2 Rows_sent: n1 Rows_examined:... (6 Replies)
Discussion started by: rakesh_411
6 Replies

5. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

6. Shell Programming and Scripting

Problems setting or exporting variable when using multiple commands from same line.

I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it. I cannot get one of the variables to display back in the main calling script... (2 Replies)
Discussion started by: scottrif
2 Replies

7. UNIX for Dummies Questions & Answers

Multiple Commands on a Single Line

Hi There, I have a cronjob that executes a small script (few lines) that I am certain can be achieved in a single line. The functional objective is actually really simple; cmd var1 The '1' in 'var1' is actually derived from date (day of month) but the snag is when working with 1-9 I... (3 Replies)
Discussion started by: Random79
3 Replies

8. Shell Programming and Scripting

Combine two lists From Multiple Grep commands.

I'm working with a file with an xml structure. I'd like to parse it down to just the bits i want. Here is and example of the file <message id="96352877" method="status"> <date rfc="Sat, 12 Mar 2011 16:13:15 -0600" unix="1299967995" /> <services> <service id="facebook"... (4 Replies)
Discussion started by: Erulisseuiin
4 Replies

9. Shell Programming and Scripting

Combine multiple lines in single line

This is related to one of my previous post but now with a slight difference: I need the "Updated:" to be in one line as well as the "Information:" on one line as well. These are in multiple lines right now as seen below. These can have 2 or more lines that needs to be in one line. System name:... (8 Replies)
Discussion started by: The One
8 Replies

10. Shell Programming and Scripting

Putting multiple sed commands on a single line

Hi, I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following: sed '1,7w file1; 8,$w file2' fileA I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Discussion started by: varelg
1 Replies
Login or Register to Ask a Question