Tcsh Problems with # and []


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tcsh Problems with # and []
# 1  
Old 09-13-2014
Tcsh Problems with # and []

Hallo,

I try to write a program which processes an input-file linewise.
I created the following minimal example:

hash-problem:
Code:
#!/bin/tcsh
foreach text ("`cat $1`")
      echo $text
end

Usually, it works as expected, but there are two problems:

1. If the argument of hash-problem has an #, then I get an error:
Code:
localhost:~/scripts/tcsh.text$ hash-problem ex#1.txt 
cat: ex: No such file or directory
Exit 1
Exit 1

2. If the file ex1.txt includes some line with [], for example
"eoi oij4g 4g4g [hde] vrrvw", then I get an error:
Code:
localhost:~/scripts/tcsh.text$ hash-problem ex2.txt
echo: No match.
Exit 1
Exit 1

Best regards,
Daniel
# 2  
Old 09-14-2014
The 2. problem is avoided by
Code:
echo "$text"

The 1. problem perhaps by
Code:
foreach text ("`cat "$1"`")

or by
Code:
foreach text ("`cat $1:q`")

--
Consider rewriting your script in shell!
Code:
#!/bin/sh
while read text
do
  echo "$text"
done < "$1"


Last edited by MadeInGermany; 09-14-2014 at 03:28 AM..
# 3  
Old 09-14-2014
Quote:
Originally Posted by MadeInGermany
Consider rewriting your script in shell!
Code:
#!/bin/sh
while read text
do
  echo "$text"
done < "$1"

Tcsh is a shell. /bin/sh generally refers to the Bourne shell - which is a ever older shell than the Tenex C shell. I suspect you meant to recommend using a more modern shell such as the Bash shell.
# 4  
Old 09-14-2014
The above solution by MadeInGermany for the 2nd problem
works very well. Thanks alot.

However, both suggestions for the 1st problem do not work,
I still get the same error.
For now, I found a clumpsy solution: In the beginning, the script changes all #'s in the current directory to _'s, and at the
end, it renames _'s back to #'s.

Daniel
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tcsh to sh

Dear all, I have piece of command from tcsh, which I would like to be in my .bashrc file. However, I am comletely blank about the tcsh commandline. if (-e ~/forum/dir/code.sh) then source ~/forum/dir/code.sh endif Any piece of suggestions how to convert it to sh way? Thank you emily (5 Replies)
Discussion started by: emily
5 Replies

2. Shell Programming and Scripting

Help in tcsh script

Hi All, I wrote a tcsh script, but being a beginner it took me lots of efforts and on top of that I am still struggling with little modifications here and there. kindly have a loop. Line1 : I want it to run maximum of "Max" Which I am providing outside loop. So how the "for" should be... (10 Replies)
Discussion started by: nrjrasaxena
10 Replies

3. Shell Programming and Scripting

perl in tcsh

hi, I am completely new for tcsh scripting. Trying to write a code, need to implement following function there, perl -pi.bak -e 's/filei/file(i+1)/g' data I want the "i" to increment and this should change the file name as i = 1; file1->file2 i=2; file2->file3 and so on also... (4 Replies)
Discussion started by: nrjrasaxena
4 Replies

4. Shell Programming and Scripting

tcsh help

Does anyone no way my .tcsh_history file is filling up with a bunch of crap?? It is filled with lines like: ! ls eccracrascratcd ! ls mecd /hchoo "cratch2/mecd /sch2/mecd /sh2/mecd /scratchcd /scratch2/mecd /scratcraecd /ls mo "ls" > ! ls eccratch2/mecd /sc/ls"d /scratch2/mecd histecho "ls" o... (2 Replies)
Discussion started by: Bic121
2 Replies

5. Shell Programming and Scripting

logging out in tcsh

Hi! I want to log out of the tcsh shell without updating the history? Thanks, Jack. (3 Replies)
Discussion started by: jacki
3 Replies

6. UNIX for Dummies Questions & Answers

tcsh issues

HI, I am having strange issues with my tcsh shell. First, the "ln" command doesnt seem to work properly. I have a file "target" that is pointing to "file1". I cannot access file1 but that shouldnt matter. when I do this, ln -sf file2 target I get permission denied that I cannot access the... (1 Reply)
Discussion started by: sardare
1 Replies

7. UNIX for Dummies Questions & Answers

About tcsh shell

Hello, Why tcsh shell is not recommended ? then which one is better ? Also can you please let me know how to change own shell and config file? (3 Replies)
Discussion started by: darshakraut
3 Replies

8. UNIX for Dummies Questions & Answers

help in tcsh

am working in tcsh while writing a script, what is diff between foll two starting line #!/bin/csh #!/bin/csh -f Also can I use the same line for script in tcsh or I have to necessarily use #!/bin/tcsh I guess even #!/bin/sh will also do. Kindly clarify (3 Replies)
Discussion started by: mahendrakamath
3 Replies

9. Shell Programming and Scripting

tcsh

I'm working on OpenOffice Localization; In that I need to work most of in 'tcsh' Since I have almost work till now in 'bash', I want to explore 'tcsh' much more .. An body suggest me a way ? books ? Thanks, :) (1 Reply)
Discussion started by: kartik
1 Replies

10. UNIX for Dummies Questions & Answers

tcsh and redirect

Hello, maybe it is a silly question, but can somebody tell me how to do a redirect of errors in the t shell? Unix Newbie :) (1 Reply)
Discussion started by: Micky
1 Replies
Login or Register to Ask a Question