LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2009, 08:09 PM   #1
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Rep: Reputation: 0
Joining command in csh


Hi all. I did a search for this but did not see anything relevant.

I'm trying to join several strings and execute them as a command but csh does not seem to recognize it as a command, anyone know what I'm doing wrong? numone and numtwo will be passed into the script. (I have to use csh because it is a part of an existing script.)

Code:
#!/bin/csh
set file1=file.dat
set file2=file2.dat
set numone=2
set numtwo=10
set partone='sed '
set parttwo=,
set partthree=d' $file1 > $file2
set cmd=`$partone$numone$parttwo$numtwo$partthree`
WHAT I WANT EXECUTED.
Code:
sed '2,10d' file.dat > file2.dat
When I try to run the script, sed complains about syntax.
 
Old 02-03-2009, 11:35 PM   #2
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Anyone see any mistakes?
 
Old 02-04-2009, 01:54 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Here is a working version:
Code:
#!/bin/csh
set file1=file.dat
set file2=file2.dat
set numone=2
set numtwo=10
set partone="sed '"
set parttwo=","
set partthree="d' $file1 > $file2"
set cmd="$partone$numone$parttwo$numtwo$partthree"
eval $cmd
I added some double quotes to embed strings, useful for strings with spaces and single quotes within them. I think the syntax error was the set partthree line, where you had a redirection after a set command. Embedding the whole string in double quotes solves the problem.
 
Old 02-04-2009, 03:45 PM   #4
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks! That worked like a charm. I have another that isn't working even with the quotes, any ideas?

Code:
#!/bin/csh
set filename=file.dat
set num=10
set partone="more $filename | grep -n 'RUN ("
set parttwo="' | awk -F ':' '{print $1}'"
set numone="$partone$num$parttwo"
eval $numone
WHAT SHOULD BE EXECUTED.
Code:
more file.dat | grep -n 'RUN (10' | awk -F ':' '{print $1}'
 
Old 02-05-2009, 12:26 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have two problems here: the $1 part of the awk command and the brackets {}. To solve the brackets issue (since they have a special meaning in C-shells) you have to put double quotes around the variable name in the eval command:
Code:
eval "$numone"
The $1 part is the more difficult, since in C-shells you cannot escape the dollar sign using a backslash, as you should do in bash. You have to escape it using single quotes, but since the statement contains other quotes, it's difficult to see what to escape and what not. As a workaround I'd split the command in more parts, leaving the $1 alone:
Code:
#!/bin/csh
set filename=file.dat
set num=10
set partone="more $filename | grep -n 'RUN ("
set parttwo="' | awk -F ':' '{print "
set field='$1'
set partthree="}'"
set numone="$partone$num$parttwo$field$partthree"
eval "$numone"
 
Old 02-05-2009, 05:19 PM   #6
tboss888
LQ Newbie
 
Registered: Dec 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
You have two problems here: the $1 part of the awk command and the brackets {}. To solve the brackets issue (since they have a special meaning in C-shells) you have to put double quotes around the variable name in the eval command:
Code:
eval "$numone"
The $1 part is the more difficult, since in C-shells you cannot escape the dollar sign using a backslash, as you should do in bash. You have to escape it using single quotes, but since the statement contains other quotes, it's difficult to see what to escape and what not. As a workaround I'd split the command in more parts, leaving the $1 alone:
Code:
#!/bin/csh
set filename=file.dat
set num=10
set partone="more $filename | grep -n 'RUN ("
set parttwo="' | awk -F ':' '{print "
set field='$1'
set partthree="}'"
set numone="$partone$num$parttwo$field$partthree"
eval "$numone"

Thanks again. In order to execute the code I had to change one thing.
I used:

Code:
set numone=`"$partone$num$parttwo$field$partthree"`
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
csh command log like .bash_history ivanatora Linux - General 3 05-14-2008 05:34 AM
!command expansion in csh vs. bash jhwilliams Linux - Software 9 08-27-2007 05:04 PM
csh nirmaltom Programming 1 05-18-2007 08:07 AM
any command line software for ogg concatenation but ogmcat.? Joining ogg files Emmanuel_uk Linux - Software 1 06-15-2006 09:52 AM
newgrp command within perl/csh script bobsey Programming 1 04-13-2001 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration