Previous Table of Contents Next


EXAMPLE C SHELL SCRIPTS

Here is an interactive C Shell version of the uucp command.

     #!/bin/csh -f
     #
     #    cuucp - C Shell interactive UUCP
     #
     #    Anatole Olczak - ASP, Inc

     # Set interrupt
     onintr quit

     # Get pubdir from environment, or set to default
     if (${?pubdir}) set pubdir=/usr/spool/uucpublic

     # Set uucp Systems file, depending on UUCP version
     if (-e /usr/lib/uucp/Systems) then
          set uufile=/usr/lib/uucp/Systems
     else if (-e /usr/lib/uucp//L.sys) then
          set uufile=/usr/lib/uucp/L.sys
     else if (-e /etc/uucp/L.sys) then
          set uufile=/etc/uucp/L.sys
     endif

     # No uucp, or unknown version
     if (!(${?uufile})) then
          echo "No UUCP Systems file\!"
          goto quit
     endif

     # Prompt for source file name
     echo -n "Enter source file: "
     set source=$<

     # Make sure source file exists
     if (!(-e ${source})) then
         echo "${source}: non-existent or not accessible"
         goto quit
     endif

     # Prompt for remote system name
     echo -n "Enter remote systemname: "
     set remote=$<

     # Check if remote system name is valid
     set ifexists=`grep ^${remote} ${uufile}`
     if (!(${?ifexists})) then
          echo "${remote}: invalid system name"
          goto quit
     endif

     # Start the uucopy
     echo "Copying ${source} to      ${remote}\!${pubdir}/${source}"
     uucp ${source} ${remote}\!${pubdir}/${source}

     # Exit with successful exit status
     exit 0

     # Exit with unsuccessful exit status
     quit:exit 1

Here is the C Shell version of the UNIX dirname command. It returns a pathname minus the last directory.

     #!/bin/csh -f
     #
     #       cdirname - C Shell dirname
     #
     #       Anatole Olczak - ASP, Inc
     #

     # Check arguments
     if (($#argv>1)) then
          echo "Usage: $0 string"
          exit 1
     endif

     # Set variables
     set base=$argv[1]

     # Display output
     echo $base:h

This C Shell command returns the last part of a pathname.

     #!/bin/csh -f
     #
     #       cbasename - C Shell basename
     #
     #       Anatole Olczak - ASP, Inc
     #

     # Check arguments
     if (($#argv<1) || ($#argv>2)) then
          echo "Usage: $0 string [suffix]"
          exit 1
     endif

     # Set variables
     set delsuffix
     set base=$argv[1]

     if (($#argv>1)) then
          set suffix=$argv[2]
          set delsuffix=yes
     endif

     # Display output
     if (($delsuffix != yes)) then
          echo $base:t
     else
          echo $base:t | sed -e 's/'$suffix'$//'
     endif

The ccal script implements a menu-driven calendar program. It supports addition, deletion, modification, and listing of calendar entries. It also provides the ability to find the calendar entry for the current day and list all calendar entries.

     #!/bin/csh -f
     #
     #       ccal - C Shell calendar program
     #
     #       Anatole Olczak - ASP, Inc
     #

     # Set commands; use environment setting or default
     if (!(${?clear})) set clear=/bin/clear
     if (!(${?editor})) set editor=/bin/vi

     # Use environment variable setting or default
     if (!(${?calfile})) set calfile=$HOME/.calfile

     # Set filenames
     set deletefile=/tmp/.delete$$
     set changefile=/tmp/.change$$
     set foundfile=/tmp/.found$$

     # Set variables
     set prompt="Press <Return> to continue"
     set operation

     # Set interrupt
     onintr quit

     # Check arguments
     if (($#argv>0)) then
          echo "Usage: $0"
          exit 1
     endif

     # Display menu
     display:
          set calfile=$HOME/.calfile
          $clear
   
     while (1)
         echo "    *** CALENDAR PROGRAM *** "
         echo ""
         echo " 1) Add calendar entry"
         echo " 2) Delete calendar entry"
         echo " 3) Change calendar entry"
         echo " 4) Find calendar entry"
         echo " 5) List all calendar entries"
         echo " 6) List todays calendar entry"
         echo " 7) Exit"
         echo ""
         echo -n "Enter selection or <Return> for default menu: "
         set i=$<

         # Parse selection
         switch($i)

                case 1:
                       set operation=add
                       goto checkdate
                       goto addentry
                case 2:
                       set operation=del
                       goto checkdate
                       goto delentry
                case 3:
                       set operation=change
                       goto checkdate
                       goto changeentry
                case 4:
                       set operation=find
                       goto checkdate
                       goto findentry
                case 5:
                       goto formatentry
                case 6:
                       set date=`date +%m%d%y`
                       goto findentry
                case 7:
                       goto quit
                       default:
                       $clear
                       echo "Invalid selection"
                       echo -n "$prompt"
                       set tmp=$<
                       $clear
                       goto display

               endsw
          end

     # Check date
     checkdate:
          $clear
          echo -n "Enter date in mmdd[yy] format (default today): "
          set date=$<

          # Verify date format
          switch ($date)

              # Default - use todays date
              case "":
                     set date=`date +%m%d%y`
                     breaksw

     # Check given date
     case [0-9]*:
          set length=`echo $date | wc -c | sed -e 's/^    //p'`

          # Make sure entire format is ok
          switch ($length)
             case 5:
                set mo=`echo $date | cut -c1,2`
                set da=`echo $date | cut -c3,4`
                set yr=`date +%y`
                set date=$mo$da$yr
                breaksw
             case 7:
                set mo=`echo $date | cut -c1,2`
                set da=`echo $date | cut -c3,4`
                set yr=`echo $date | cut -c5,6`
                set date=$mo$da$yr
                breaksw
             default:
                $clear
                echo "$date: invalid format - try again"
                echo -n "$prompt"
                set tmp=$<
                goto checkdate
     endsw

     # Now check individual value for day
     if (($da < 1) || ($da > 31)) then
        $clear
        echo "${da}: invalid day format - try again"
        echo -n "$prompt"
        set tmp=$<
        goto checkdate

     endif
   
         # Now check individual value for month
         if (($mo < 1) || ($mo > 12)) then
                $clear
                echo "${mo}: invalid month format - try again"
                echo -n "$prompt"
                set tmp=$<
                goto checkdate
             endif
             breaksw

         # Invalid date given
         default:
             $clear
             echo "$date: invalid format - try again"
             echo -n "$prompt"
             set tmp=$<
             goto checkdate
     endsw

     # Proceed to the correct operation
     switch ($operation)
          case add:
             goto addentry
          case del:
             goto delentry
          case change:
             goto changeentry
          case find:
             goto findentry
          default:
             goto display
     endsw

     # Display output
     formatentry:
          $clear

          # Check if calendar file exists first
          if (!(-z $calfile)) then
               ( awk 'BEGIN {FS=","} \
               { print "************************" } \
               { print "*                      *" } \
               { for (i=1; i<=NF; i++) \
               if (i==1) \
               printf "                       *\r* DATE: %s\n", $i \
               else \
               printf "                       *\r* %s\n", $i } \
               { print "*                      *" } \
               END { print "************************" }' $calfile ) | more

          # No calendar file, so go back to the top
          else
               echo "No entries found"
               echo -n "$prompt"
               set tmp=$<
               goto display
          endif

          # Prompt to continue
          echo -n "$prompt"
          set tmp=$<

          # Go to display menu function
          goto display

     # Find specific entry
     findentry:
          $clear

          # Find entry and put it in temp file
          grep "^$date" $calfile >$foundfile

          # Reset calfile to temp file and format output
          set calfile=$foundfile
          goto formatentry

     # Create new entry
     addentry:
          $clear
          set entry=$date

          # Make sure entry does not already exist
          set count=`grep -c "^$date" $calfile`

          # If entry exists, then prompt to change entry
          if (($count > 0)) then
                goto changeentry
          endif

          # Prompt for entry input
          echo "Enter info for ${date}: (enter <Return> by itself when finished)"
          while (1)
               echo -n "=>"
               set line=$<

               if (("$line" != "")) then
                     set entry="${entry},${line}"
               else
                     break
               endif
           end

           # Append entry to calfile
           echo $entry>>$calfile

           # Resort calendar file
           sort -o $calfile $calfile

           goto display

     # Delete entry
     delentry:
          set reply="yes"
          $clear

          # Make sure requested entry exists
          grep "^$date" $calfile >$foundfile

          if ((-z $foundfile)) then
              $clear
              echo "Entry for <$date> not found"
              echo -n "$prompt"
              set tmp=$<
              goto display
          endif

     # Double check before deleting
     while (($reply != ""))

          echo -n "Delete entry for <$date>?"
          set reply=$<

          # Parse answer
          switch ($reply)
                 case [yY]* :
                        break
                 case [nN]* :
                       goto display
                 case "" :
                       break
                 default :
                       breaksw
          endsw
     end

     # Put all entries, except deleted in temp file
     grep -v "^$date" $calfile > $deletefile

     # Move temp file to calfile
     cat $deletefile > $calfile

     # Clean up temp files
     rm -rf $deletefile $foundfile

     goto display
 
     # Change entry
     changeentry:
          set reply="yes"

          # Make sure requested entry exists;
          # change field separator, commas, to newlines
          grep "^$date" $calfile | tr ',' '\012'>$foundfile

          if ((-z $foundfile)) then
              $clear
              echo "Entry for <$date> not found"
              echo -n "$prompt"
              set tmp=$<
              goto display
          endif

     # Double check before making change
     while (($reply != ""))

         echo -n "Change/Add entry for <$date>?"
         set reply=$<

         # Parse answer
         switch ($reply)
               case [yY]* :
                      break
               case [nN]* :
                      goto display
               case "" :
                      break
               default :
                      breaksw
          endsw
     end

          # Edit requested entry
          $editor $foundfile

          # Put all entries, except deleted in temp file
          grep -v "^$date" $calfile >$changefile
          # Append changed entry to temp file in correct
          # format; change newlines to comma separators
          ( cat $foundfile | tr '\012' ',' ; echo "" ) >>$changefile
    
          # Move temp file to calfile
          cat $changefile > $calfile

          # Resort calfile
          sort -o $calfile $calfile

          # Clean up temp files
          rm -rf $changefile $foundfile $deletefile

          goto display

     # Clean up files on interrupt
     quit:
          rm -rf $changefile $foundfile $deletefile


Previous Table of Contents Next
1