- How to write If-else statement in one line in csh?
- 4 Answers 4
- C Shell Scripting
- Pound Shebang
- C-Shell Command Line Arguments
- Set Variables
- C-Shell Flow Control
- Foreach loop
- Changing Directories in Script
- Show Progress
- Recent Posts
- Linux csh if and
- Bourne Shell, sh, Special Commands
- C shell, csh
- Korn Shell, ksh, Special Commands
- Korn Shell, ksh93, Special Commands
How to write If-else statement in one line in csh?
I use Cshell and i’m writing an if else statement in oneLine. If statement executed successfully and else block is going for infinite loop and cannot exit back to promt. Any suggestion how it can be solved?
/home/sun_cdmasee>if ( 25 == 25 ) then ; echo "yes" ; else echo "no" ; endif
Don’t use csh . STFW for csh considered harmful then switch (using chsh ) to some better shell like bash , zsh or fish
Come to learn more about tcsh and share your insights as well here: blogger.com/blog/posts/2001316915191381029?q=label%3Atcsh
4 Answers 4
(The words else and endif must appear at the beginning of input lines; the if must appear alone on its input line or after an else .)
Don’t try to program in csh. Don’t even use it as your interactive shell.
Incidentally, you can exit back to your normal csh prompt by typing endif at the else? prompt.
My job (EE) requires (t)csh, so you should qualify your advice with «If you have a choice, . «. EE community got stuck with csh due to EDA getting start on SunOS (csh default).
Answering this one despite being old, because this is the first Google hit, and in case anyone comes looking for it, they’re likely to end up here.
The manual might say it’s impossible, but instead of a single if, the same functionality can be just achieved with oppositely formed ifs:
if ("a" =~ "b") echo "wrong"; if ("a" !~ "b") echo "right"
Same can be applied in aliases:
alias blabla 'if ("\!:1" =~ "bla*") echo "chat"; if ("\!:1" !~ "bla*") echo "quiet"'
And yes, after a decade+ of writing small stuff in tcsh, because it’s used for just about anything, the «don’t write in csh» suggestion should come with a ton of disclaimers, or better formed like «don’t write serious scripts in csh». When it is your shell because other shells aren’t supported in the relevant industry almost anywhere, these patronizing responses aren’t doing anything useful.
C Shell Scripting
Here are some notes on C-Shell scripts. It’s not all encompassing, but it covers most of what I do with a shell script. If I need to do more then I usually switch to Perl.
Pound Shebang
The # is a pound sign, and the ! is the shebang. The #! (Pound Shebang) indicates the interpreter program that should run the script. This can be any of the many shell programs, Perl, PHP, Python or even your own custom program. I’m pretty sure all Unix shells support Pound Shebang, it’s not C-Shell specific. A C-Shell script should always start with the following line.
C-Shell Command Line Arguments
Usually a C-Shell script has one more more commandline arguments. $#argv indicates the number of arguments. This example prints a message and exits if there are less than two arguments entered.
Set Variables
The command line arguments are stored in array $argv. The first argument is in $argv[1], the second in $argv[2] and so on.
The set command assigns a value to a variable. In this example the first argument contains the sitename. Variables are set to a value without the $-sign and are accessed with the $-sign.
set sitename = $argv[1] echo $sitename
In the following example default values are assigned to indicate what tasks the script will do. In this example the default is to do nothing so all to do variables default to 0 (false). The default values may be over-ridden by command-line arguments in a subsequent example.
set do_backup = 0 set do_wget = 0 set do_gsutil = 0
C-Shell Flow Control
Foreach loop
You can loop through each argument using the foreach command. Note the variable arg does NOT have a $-sign in the foreach line, but it does have a $-sign when it is used.
foreach arg ($argv) echo "argument if"> If Statement
When an if statement has only one command to execute then you can put the if statement and the command on a single line.
IMPORTANT NOTE:
In the following example the == has a ' ' space on each side. This is needed and the script will not function correctly without these spaces. This is an annoying quirk of the C-Shell, but it's not too significant if you know about it. which you now do. foreach arg ($argv) if ($arg == '1') set do_wget = 1 if ($arg == '2') set do_backup = 1 if ($arg == '3') set do_delete = 1 end if ($do_wget) echo "do_wget" if ($do_backup) echo "do_backup" if ($do_delete) echo "do_delete"
If multiple commands are to be executed within the if statement then you need then on the if line and you need endif at the end.
if ($do_wget) then wget -r php.$ echo "Creating deleteme.script" diff -rqs php.$ php.$.orig |grep ' are identical$' |sed 's#^Files \(.*\) and .*#rm -rfv #' > deleteme.script endif
C-Shell supports the else. Note, you do NOT need the endif before the else. In the following example, nested if statements use the -e file existence check to determine what files should be copied.
if ($do_backup) then if (-e php.$) then if (-e php.$.orig) then echo "Moving 'php.$' to 'php.$.backup'" echo "You will want to move 'php.$.backup' to 'php.$.orig' so the diff/delete_unchanged phase works" cp -rf php.$ php.$.backup else echo "Moving 'php.$' to 'php.$.orig'" cp -rf php.$ php.$.orig endif endif endif
The following example runs a script if it exists. The -e file existence check is used. The ! -e indicates that the file does not exist.
if ($do_delete) then if ( -e deleteme.script ) source deleteme.script if ( ! -e deleteme.script ) echo "deleteme.script does not exist" end if
Here is a list of CShell file checks that I am aware of.
-d file file is a directory -e file file exists -f file file is an ordinary file -r file file is readable by user -o file file is owned by user -w file file is writable by user -x file file is executable by user -z file file has size 0
In the following example the foreach loop iterates through a list of numbers.
foreach i (1 2 3 4) echo $i end
- The Unix/Linux seq first step last command generates a sequence. This command is available on the command-line of most Unix/Linux shells.
- The backtick puts the stdout output of the embedded command(s) onto the command line. This is also available in most Unix/Linux shells.
In the following example, the empty sub-directories in php.1, php.2, php.3 and php.4 are found and deleted.
foreach i (`seq 1 1 4`) rm -rf `find php.$ -empty` end
The following example loops through a list of files returned by glob *.txt. A backup is made of each file.
foreach file (*.txt) cp $file $file.bck end
Changing Directories in Script
The pushd command can be used to change into a directory and the the popd command returns to the original directory after the work is completed.
Show Progress
The echo command prints text stdout.
Recent Posts
Linux csh if and
The shell command interpreters csh (1), ksh (1), ksh93 (1), and sh (1) have special built-in commands. The commands case , for , foreach , function , if , repeat , select , switch , until , and while are commands in the syntax recognized by the shells. They are described in the Commands section of the manual pages of the respective shells. In ksh93 (1), fc , hash , stop , suspend , times , and type are aliases by default. In ksh93 , the following built-ins are bound to the /bin pathname by default and are invoked if the pathname search encounters an executable command of that name in the /bin or /usr/bin directory: cat , chown , getconf , head , mkdir , rmdir , tee , uniq , and wc .
The remaining commands listed in the following table are built into the shells for reasons such as efficiency or data sharing between command invocations. They are described on their respective manual pages.
Command Shell
++**alias csh, ksh, ksh93
bg +*break builtin case cat cd chdir chown command +*continue dirs disown echo +*eval +*exec +*exit ++**export false fc fg for foreach function getconf getopts glob goto hash hashstat head hist history if jobs kill let limit login logout mkdir nice +*newgrp nohup notify onintr popd print printf pushd pwd read ++**readonly rehash repeat +*return select +set setenv shift sleep source stop suspend switch tee test time *times *+trap true type ++**typeset ulimit umask +unalias unhash uniq unlimit +unset unsetenv until *wait whence while
Bourne Shell, sh, Special Commands
Input/output redirection is now permitted for these commands. File descriptor 1 is the default output location. When Job Control is enabled, additional Special Commands are added to the shell's environment.
In addition to these built-in reserved command words, sh also uses:
: No effect; the command does nothing. A zero exit code is returned.
. filename Read and execute commands from filename and return. The search path specified by PATH is used to find the directory containing filename .
C shell, csh
Built-in commands are executed within the C shell. If a built-in command occurs as any component of a pipeline except the last, it is executed in a subshell. In addition to these built-in reserved command words, csh also uses:
: Null command. This command is interpreted, but performs no action.
Korn Shell, ksh, Special Commands
Input/Output redirection is permitted. Unless otherwise indicated, the output is written on file descriptor 1 and the exit status, when there is no syntax error, is zero.
Commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed.
In addition to these built-in reserved command words, ksh also uses:
* : [ arg . ] The command only expands parameters.
* . file [ arg . ] Read the complete file then execute the commands. The commands are executed in the current shell environment. The search path specified by PATH is used to find the directory containing file . If any arguments arg are specified, they become the positional parameters. Otherwise, the positional parameters are unchanged. The exit status is the exit status of the last command executed. the loop termination test.
Korn Shell, ksh93, Special Commands
Input/Output redirection is permitted. Unless otherwise indicated, the output is written on file descriptor 1 and the exit status, when there is no syntax error, is zero.
Except for : , true , false , echo , newgrp , and login , all built-in commands accept -- to indicate end of options. They also interpret the option --man as a request to display the manual page onto standard error and -? as a help request which prints a usage message on standard error.
Commands that are preceded by one or two + are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. They are not valid function names. 5. Words, following a command preceded by ++ that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign and field splitting and file name generation are not performed.
In addition to these built-in reserved command words, ksh93 also uses:
: [ arg . ] The command only expands parameters.
. name [ arg . ] If name is a function defined with the function name reserved word syntax, the function is executed in the current environment (as if it had been defined with the name() syntax.) Otherwise if name refers to a file, the file is read in its entirety and the commands are executed in the current shell environment. The search path specified by PATH is used to find the directory containing the file. If any arguments arg are specified, they become the positional parameters while processing the . command and the original positional parameters are restored upon completion. Otherwise the positional parameters are unchanged. The exit status is the exit status of the last command executed.