KEMBAR78
Cis 216 – shell scripting | PPTX
Remembering the basics – Week 1
   Dan Morrill April 01 2013
 RMDIR – removes a directory only if it is empty
 RMDIR –r – removes a directory regardless if it is
  empty or not
 CHMOD 777 –r /user/dan – recursively changes the
  permissions of the directory /user/dan to
  RWXRWXRWX
 grep – pattern matching for example ps –ef |grep
  apache2 will look for all processes running under the
  name apache2
   egrep – used in regular expressions
 find – find the location of a file (or files) on a hard
  drive at the command line
 echo – writes data to the screen (standard output)
   The TTY – Screen is always standard output (STDOUT)
 more – displays the contents of a file one page at a time
  (hard for long files like syslog)
 clear – clears the screen of text and provides the
  command prompt
 chown – change the owner of a file or directory
 rm – remove a file
 mv – move a file to a new place and or new name
 chgrp – change the group ownership of a file
 mkdir – make a directory
 ls – list the files in a directory, ls –la show the long
  form (including permissions and owners) of the files
  in a directory
 cat – an alternative to showing the data in a file all at
  once
 pwd – display the current directory
 passwd – change the password of the currently logged
  in user
 >> - append data to the END of a file
 > - overwrites the file and any data in it
 | - pipe character – strings commands together
 || - logical OR – in that A || B in a loop, condition A or
  condition B
 & - executes your process in the background – so you can
  still use the command line ./script.sh & will run your
  process in the background
 && - Logical AND A && B if A succeeds, run B
 sleep – stop execution of the script for a specified number
  of seconds sleep 20 means sleep for 20 seconds
 diff – compares two files (this is great for seeing what
    changes were made in a script against the original and
    modified)
   sdiff – only works with the 132 character display, but
    compares files side by side
   lp – prints a file to the configured printer
   lpstat – shows the user what is in the printer queue
   ( ) – runs the enclosed command in a sub-shell
   (( )) – evaluates and assigns a value to a variable and
    does math in a sub-shell
 $(( )) – evaluates the enclosed expression
 [ ] – test an expression to see if it is true or false
 < > - used to compare values or strings
 $( ) – command substitution – great when doing
  logical OR’s or AND’s
 ++ - auto increment a value for I=1 ++ (takes I up by 1
  for every loop)
 -- - auto decrement a value for I=100 -- (takes I down
  by 1 for every loop)
 <= - less than or equal to
 >= - greater than or equal to
 I will expect to see this in the header of every Unix Script
    you will turn in:
   #!/bin/sh (tells the computer to use a bourne shell)
   #
   # Script: Name of script (what you named this file)
   # Author: Your Name (so I know who wrote it)
   #Date: (the date you wrote it)
   #Revision: 1.1.A(how many times it changed, and what
    state it is in(A, B, D, T or P)
     All scripts should also have A, B, D, T or P at the end of the
      revision statement so I know if it is Alpha, Beta, Dev, Test, or
      Production. All scripts turned in should be production scripts
 How to get your script to do more useful stuff
   If. Then
   if [test]
       Then
        stuff I want the computer to do
      fi
   If. Then. Else
      if [test]
        Then
           stuff I want the computer to do
        Else
           Stuff I want the computer to do if the first condition didn’t happen
        fi
 for
   for A=1
      do
           stuff I want the computer to do
         done

 While
   While A=1
     do
           stuff I want the computer to do
         done
 Until
   until A=1
      do
           stuff I want the computer to do
      done

 Case
   case A=1
      stuff I want the computer to do
   case A != 1
      stuff I want the computer to do
   easc

Cis 216 – shell scripting

  • 1.
    Remembering the basics– Week 1 Dan Morrill April 01 2013
  • 2.
     RMDIR –removes a directory only if it is empty  RMDIR –r – removes a directory regardless if it is empty or not  CHMOD 777 –r /user/dan – recursively changes the permissions of the directory /user/dan to RWXRWXRWX  grep – pattern matching for example ps –ef |grep apache2 will look for all processes running under the name apache2  egrep – used in regular expressions
  • 3.
     find –find the location of a file (or files) on a hard drive at the command line  echo – writes data to the screen (standard output)  The TTY – Screen is always standard output (STDOUT)  more – displays the contents of a file one page at a time (hard for long files like syslog)  clear – clears the screen of text and provides the command prompt  chown – change the owner of a file or directory  rm – remove a file
  • 4.
     mv –move a file to a new place and or new name  chgrp – change the group ownership of a file  mkdir – make a directory  ls – list the files in a directory, ls –la show the long form (including permissions and owners) of the files in a directory  cat – an alternative to showing the data in a file all at once  pwd – display the current directory  passwd – change the password of the currently logged in user
  • 5.
     >> -append data to the END of a file  > - overwrites the file and any data in it  | - pipe character – strings commands together  || - logical OR – in that A || B in a loop, condition A or condition B  & - executes your process in the background – so you can still use the command line ./script.sh & will run your process in the background  && - Logical AND A && B if A succeeds, run B  sleep – stop execution of the script for a specified number of seconds sleep 20 means sleep for 20 seconds
  • 6.
     diff –compares two files (this is great for seeing what changes were made in a script against the original and modified)  sdiff – only works with the 132 character display, but compares files side by side  lp – prints a file to the configured printer  lpstat – shows the user what is in the printer queue  ( ) – runs the enclosed command in a sub-shell  (( )) – evaluates and assigns a value to a variable and does math in a sub-shell
  • 7.
     $(( ))– evaluates the enclosed expression  [ ] – test an expression to see if it is true or false  < > - used to compare values or strings  $( ) – command substitution – great when doing logical OR’s or AND’s
  • 8.
     ++ -auto increment a value for I=1 ++ (takes I up by 1 for every loop)  -- - auto decrement a value for I=100 -- (takes I down by 1 for every loop)  <= - less than or equal to  >= - greater than or equal to
  • 9.
     I willexpect to see this in the header of every Unix Script you will turn in:  #!/bin/sh (tells the computer to use a bourne shell)  #  # Script: Name of script (what you named this file)  # Author: Your Name (so I know who wrote it)  #Date: (the date you wrote it)  #Revision: 1.1.A(how many times it changed, and what state it is in(A, B, D, T or P)  All scripts should also have A, B, D, T or P at the end of the revision statement so I know if it is Alpha, Beta, Dev, Test, or Production. All scripts turned in should be production scripts
  • 10.
     How toget your script to do more useful stuff  If. Then  if [test]  Then  stuff I want the computer to do  fi  If. Then. Else  if [test]  Then  stuff I want the computer to do  Else  Stuff I want the computer to do if the first condition didn’t happen  fi
  • 11.
     for  for A=1  do  stuff I want the computer to do  done  While  While A=1  do  stuff I want the computer to do  done
  • 12.
     Until  until A=1  do  stuff I want the computer to do  done  Case  case A=1  stuff I want the computer to do  case A != 1  stuff I want the computer to do  easc