Friday, 20 April 2007

shell_script

Environment Variables: used by shell and by other programs:


  • [zliybsj2@unnc-cslinux ~]$ echo $PATH
  • /usr/kerberos/bin:/usr/local/bin:
  • /bin:/usr/bin:/usr/X11R6/bin:/opt/SUNWappserver/jdk/bin/:
  • /usr/NX/bin:/home/domain/zliybsj2/bi
  • [zliybsj2@unnc-cslinux ~]$ echo $TERM
  • xterm
  • [zliybsj2@unnc-cslinux ~]$ echo $HOME
  • /home/domain/zliybsj2
  • [zliybsj2@unnc-cslinux ~]$ echo $PS1
  • [\u@\h \W]\$
  1. using all capitals,
  2. using echo to get the value of a variable.
  3. variables are expanded by the shell before he command is executed.
  4. environment variables can be used by any program.
  5. variables change is only visible in the current shell.child process don't automatically inherit the environmrnt variables from their parent. using export to let proecessed get the changed value, eg export VAR=value, then $VAR in child process is value, or stay same. using : to add somthing to PATH, eg, export PATH=$PATH:new stuff

PATH:

  1. it tells UNIX where to look for executables
  2. when you type the name of a program, the shell will look for it in your PATH
  3. ecah directory seperated by a colon
  4. changed PATH can only live in current widow.

shell script:

  1. the shell is a command interpreter
  2. shell script is a text file contains all command you want to group, which is executed in a sub shell.
  3. first line is a specially comment: #!/bin/sh tell program which kind of shell will be using to execut this shell.

chomd :

  1. chmod o-r coursework
  2. chmod u+x shellscript
  3. chmod a+x shellscript
  4. chmod +x directory

input and output:

  • echo works like Java's println.
  • argument: $0, $1, $2,...... $#

conditionals:

  • if ...
  • then ...
  • else if ...
  • else ...
  • fi

read: to get input to interacte with users

can have more than one argument. will bind the first word of input to the first variable and bind the rest to the second. this acts like a list or array, so can be used with for.

controlling input:

  • using <<>
  • using <>

No comments: