Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a tcsh shell command and selectively ignore the status?

Tags:

shell

unix

csh

tcsh

I've got a tcsh shell script that I would like to stop with an error on nonzero status most of the time, but in some cases I want to ignore it. For example:

#!/bin/tcsh -vxef

cp file/that/might/not/exist . #Want to ignore this status
cp file/that/might/not/exist . ; echo "this doesn't work"
cp file/that/must/exist . #Want to stop if this status is nonzero
like image 438
Walter Nissen Avatar asked Jun 14 '10 23:06

Walter Nissen


People also ask

How do I run tcsh in shell?

You can invoke the shell by typing an explicit tcsh command. A login shell can also be specified by invoking the shell with the -l option as the only argument. A login shell begins by executing commands from the system files /etc/csh. cshrc and /etc/csh.

What does tcsh command do?

Tcsh is an enhanced version of the csh. It behaves exactly like csh but includes some additional utilities such as command line editing and filename/command completion.


1 Answers

I don't know about tcsh, but with bash, you can use set -e to do this. When the -e flag is set, bash will exit immediately if any subcommand fails (see the manual for technical details). When not set, it will continue to execute. So, you can do something like this:

set +e
cp file/that/might/not/exist .  # Script will keep going, despite error
set -e
cp file/that/might/not/exist .  # Script will exit here
echo "This line is not reached"
like image 156
Adam Rosenfield Avatar answered Sep 24 '22 21:09

Adam Rosenfield