tcsh: how to prevent a foreach from terminating a script when the result is null?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tcsh: how to prevent a foreach from terminating a script when the result is null?
# 1  
Old 01-26-2012
tcsh: how to prevent a foreach from terminating a script when the result is null?

Sorry if this has been answered. I did search both Google and this site and did find this post: unix.com/unix-dummies-questions-answers/152992-how-ignore-errors-script.html

However, it wasn't answered.

I have the same question - how do you prevent a tcsh script from terminating when the result of a foreach is null?

So if my script does something like:

Code:
foreach file ( *.tif )
   mv $file temp/$file
end

foreach file ( *.jpg )
   mv $file temp/$file
end

If the first foreach doesn't match anything, the entire script terminates with the error:

foreach: No match.

How do you prevent this? Do you have to do a check for the presence of anything you need to do a foreach on?
# 2  
Old 01-27-2012
Code:
 
for i in *.tif
do
    mv $i /tmp/$i
done

# 3  
Old 01-27-2012
Quote:
Originally Posted by itkamaraj
Code:
 
for i in *.tif
do
    mv $i /tmp/$i
done


Thanks, but is that your way of telling me to code in bash instead? I'm really looking for help with the foreach in tcsh.
# 4  
Old 01-27-2012
i never coded in tcsh. sorry Smilie

https://www.unix.com/unix-dummies-que...rs-script.html

Csh Programming Considered Harmful

http://www.grymoire.com/Unix/CshTop10.txt

---------- Post updated at 02:26 PM ---------- Previous update was at 02:11 PM ----------

if you set the "nonomatch" variable:
Code:
set nonomatch

then the C shell behaves like the Bourne Shell.
This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 01-30-2012
Quote:
Originally Posted by itkamaraj

if you set the "nonomatch" variable:
Code:
set nonomatch

then the C shell behaves like the Bourne Shell.
Ah, that seems to do the trick. Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get following result using foreach command?

Dear all, For example, if data contains following numbers:1 2 3 4 5 6 I would like get like below using foreach command,1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 (1 Reply)
Discussion started by: Ryan Kim
1 Replies

2. UNIX for Advanced & Expert Users

why the script is not terminating with proper results

Hi everyone, I am new to the linux.I wrote a small script and assigning two values to fname and lname and I want if the fname or lname are not given proper name like Toys or Gun the script should terminate and if they are given proper name it should execute.please help thanks:wall: #!/bin/bash... (4 Replies)
Discussion started by: starter2011
4 Replies

3. Shell Programming and Scripting

for / foreach syntax issues (in bash or tcsh)

So I am new to unix, and actually anything outside drag and drop with the mouse (been learning for about a week so far) . I have been using the foreach command in tcsh because I am working on a group of files. Basically what I need is to insert part of the filename as the first line in the file.... (0 Replies)
Discussion started by: thepolypore
0 Replies

4. Shell Programming and Scripting

prevent ssh from executing result in shell

Hi, I am writing a script on Solaris 10 and want to execute a remote ssh command. Normally this command should just return the value 0000000000002356 but when using ssh it seems it is passing the result to the shell to execute. ssh root@10.5.112.145 `/usr/bin/nawk -F\, '$1=="USG" && $2=="01"... (3 Replies)
Discussion started by: borderblaster
3 Replies

5. Shell Programming and Scripting

How can i terminating expect script without terminating SSH connection.

Hi all , i know i ask a lot of question but these are really hard to solve and important question. I send two scripts: expect.sh: #!/usr/local/bin/expect spawn ssh root@172.30.64.163 expect "login:" send "root\n" expect "password:" send "root\n^M" interact and son.sh: ... (2 Replies)
Discussion started by: fozay
2 Replies

6. Shell Programming and Scripting

simple tcsh question using foreach

I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. I'm doing this in tcsh This works fine to do the search: while read i; do grep $i file2; done <file1.txt This also works fine to read a directory: foreach file ('/bin/ls... (1 Reply)
Discussion started by: moldoverb
1 Replies

7. Shell Programming and Scripting

script unexpectedly terminating

I now that this isnt the greatest code around. Im a network guy by trade not a programer .. but needed something to compare config files ... Anyway ... intermittently, the program terminates. Ive been looking at the code for a week trying to figure it out and Im stumped. Can anyone provide... (0 Replies)
Discussion started by: popeye
0 Replies

8. UNIX for Dummies Questions & Answers

Terminating child script with terminating the parent script

Hi I was working on a shell script with randomly shows a page of text from a randomly selected topic .As soon as the page is displayed it callers a timer script which keeps on running indefinitely until the timer script is killed by the user. This is where I have the problem,if I press... (2 Replies)
Discussion started by: mervin2006
2 Replies

9. Shell Programming and Scripting

terminating script with CTRL+D

Hi, I'm trying to make a script that reads the console input and terminates with CTRL+D. It's absolutely basic but I don't know how to "read" the CTRL+D. I've tried a bunch of things like EOT=^D while //with & without quotations do read input echo $input done while while ] ... (12 Replies)
Discussion started by: sanchopansa
12 Replies
Login or Register to Ask a Question