Project84.A Bash and Tcsh Reference


Project 84. A Bash and Tcsh Reference

"What's the correct syntax for . . . ?"

This project looks at the syntax of common shell commands such as variable assignment, redirection, and shell scripting statements. It shows the syntax for Bash and Tcshthe two shells that are used most often in Mac OS X Unix.

Learn More

Project 5 compares the various shell flavors.


Learn More

Project 4 covers shell variables and environment variables, and how they differ.


Set Variables

Table 9.2 shows you how to set shell variables and environment variables.

Table 9.2. Setting Variables

Set

Bash

Tcsh

Shell variable

variable=value

set variable = value

Environment variable

ENVVAR=value; export ENVVAR

setenv ENVVAR value

Environment variable (Bash only)

declare -x ENVVAR=value

 


Redirection and Pipelining

Table 9.3 shows the syntax employed by both shells to express redirection and pipelining.

Table 9.3. Syntax for Redirection and Pipelining

Redirect or Pipe

Bash

Tcsh

stdout

cmd > file

cmd > file

stderr

cmd 2> file

(cmd >/dev/tty) >& file

stdout appending

cmd >> file

cmd >> file

stderr appending

cmd 2>> file

(cmd > /dev/tty) >>& file

stdout with clobber

cmd >| file

cmd >! file

stderr with clobber

cmd 2>| file

(cmd > /dev/tty) >&! file

Both to same file

cmd &> file

cmd >& file

Both to different files

cmd > out 2> err

(cmd > out) >& err

Merge stdout into stderr

cmd 1>&2

n/a

Merge stderr onto stdout

cmd 2>&1

n/a

stdin

cmd < file

cmd < file

Pipe stdout

cmd1 | cmd 2

cmd1 | cmd2

Pipe both

cmd1 2>&1 | cmd2

cmd1 |& cmd2


Learn More

Project 6 covers the concepts of redirection and pipelining.


Tee Time

To see the output of a command onscreen and redirect it to a file, use the tee command.

$ ls Sites | tee list.txt images index.html $ cat list.txt images index.html


To redirect to multiple files, just type the names of the files as arguments. Apply option -a to append to the output files rather than overwrite them.

Startup Files

The following script files are executed by the Bash shell when it starts up. For login shells (or shells started with the command bash --login), they are

  • /etc/profile

  • ~/.bash_profile

Note

Noninteractive Bash shells do not execute any startup files.


Learn More

Project 47 covers the shell startup sequence.


For non-login shells, they are

  • /etc/bashrc (though the Bash manual claims otherwise)

  • ~/.bashrc

The following script files are executed by the Tcsh shell when it starts up. For login shells (or shells started with the command tcsh -l), they are

  • /etc/csh.cshrc

  • /etc/csh.login

  • ~/.tcshrc

  • ~/.login

For non-login shells, they are

  • /etc/csh.cshrc

  • ~/.tcshrc

Control Constructs

Syntax for each Bash and Tcsh control construct is illustrated in the following examples. All the scripts actually work, so you can play around with them.

The if Construct

#!/bin/bash if [ "$1" = "positive" ]; then   echo "Yes" elif [ "$1" = "negative" ]; then   echo "No" else   echo "Not sure" fi #!/bin/tcsh if ("$1" == "positive") then   echo "Yes" else if ("$1" == "negative") then   echo "No" else   echo "Not sure" endif


The case/switch Construct

#!/bin/bash case "$1" in   "positive")     echo "Yes"   ;;   "negative")     echo "No"   ;;   *)     echo "Not sure"   ;; esac #!/bin/tcsh switch ("$1")   case "positive":     echo "Yes"   breaksw   case "negative":     echo "No"   breaksw   default:     echo "Not sure"   breaksw endsw


Learn More

Project 10 gives examples of control constructs in a shell script.


The for Loop

#!/bin/bash for word in hello goodbye au-revoir; do   echo $word done #!/bin/tcsh foreach word (hello goodbye au-revoir)     echo $word end


The while Loop

#!/bin/bash n=0 while [ ! $n = 10 ]; do   echo $n   n=$(expr $n + 1) done #!/bin/tcsh set n = 0 while ($n != 10)   echo $n   set n = `expr $n + 1` end





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net