Table of Contents |
$ wc -lm
$ wc -lc
/home/ranga
cp -r /usr/local /opt/pgms
cp -r /usr/local /opt/pgms ; rm -r /usr/local
$ rm -r backup
/dev/rdsk/c0t1d0 | character special file |
/etc/passwd | regular file |
/usr/local | directory |
/usr/sbin/ping | regular file |
/dev/rdsk/c0t1d0 | owner bin | group sys |
/etc/passwd | owner root | group sys |
/usr/local | owner bin | group bin |
/usr/sbin/ping | owner root | group bin |
/dev/rdsk/c0t1d0 | owner read and write |
group read | |
other none | |
/etc/passwd | owner read |
group read | |
other read | |
/usr/local | owner read, write, and execute |
group read, write, and execute | |
other read, write, and execute | |
/usr/sbin/ping | owner read and SUID execute |
group read and execute | |
other read and execute |
${adams[5]}
${adams[@]}
$ ls *hw[0-9][0-9][2-6].???
$ echo "It's <party> time!"
$ echo "$USER owes \$$DEBT"
$ test -d /usr/bin || test -h /usr/bin $ [ -d /usr/bin ] || [ -h /usr/bin ] $ test -d /usr/bin -o -h /usr/bin $ [ -d /usr/bin -o -h /usr/bin ]
case "$ANS" in [Yy]|[Yy][Ee][Ss]) ANS="y" ;; *) ANS="n" ;; esac
x=0 while [ $x -lt 10 ] do x=$(($x+1)) y=0 while [ $y -lt $x ] ; do echo "$y \c" y=$(($y+1)) done echo done
#!/bin/bash select FILE in * "Exit Program" do if [ -z "$FILE" ] ; then continue ; fi if [ "$FILE" = "Exit Program" ] ; then break ; fi if [ ! -f "$FILE" ] ; then echo "$FILE is not a regular file." continue fi echo $FILE cat $FILE done
#!/bin/sh USAGE="Usage: `basename $0` [-c|-t] [files|directories]" if [ $# -lt 2 ] ; then echo "$USAGE" ; exit 1 ; fi case "$1" in -t|-x) TARGS=${1}vf ; shift for i in "$@" ; do if [ -f "$i" ] ; then FILES='tar $TARGS "$i" 2>/dev/null' if [ $? -eq 0 ] ; then echo ; echo "$i" ; echo "$FILES" else echo "ERROR: $i not a tar file." fi else echo "ERROR: $i not a file." fi done ;; -c) shift ; TARGS="-cvf" ; tar $TARGS archive.tar "$@" ;; *) echo "$USAGE" exit 0 ;; esac exit $?
#!/bin/sh USAGE="Usage: 'basename $0' [-v] [-x] [-f] [filename] [-o] [filename]"; VERBOSE=false EXTRACT=false while getopts f:o:x:v OPTION ; do case "$OPTION" in f) INFILE="$OPTARG" ;; o) OUTFILE="$OPTARG" ;; v) VERBOSE=true ;; x) EXTRACT=true ;; \?) echo "$USAGE" ; exit 1 ;; esac done shift `echo "$OPTIND - 1" | bc` if [ -z "$1" -a -z "$INFILE" ] ; then echo "ERROR: Input file was not specified." exit 1 fi if [ -z "$INFILE" ] ; then INFILE="$1" ; fi : ${OUTFILE:=${INFILE}.uu} if [ -f "$INFILE" ] ; then if [ "$EXTRACT" = "true" ] ; then if [ "$VERBOSE" = "true" ] ; then echo "uudecoding $INFILE... \c" fi uudecode "$INFILE" ; RET=$? else if [ "$VERBOSE" = "true" ] ; then echo "uuencoding $INFILE to $OUTFILE... \c" fi uuencode "$INFILE" "$INFILE" > "$OUTFILE" ; RET=$? fi if [ "$VERBOSE" = "true" ] ; then MSG="Failed" ; if [ $RET -eq 0 ] ; then MSG="Done." ; fi echo $MSG fi else echo "ERROR: $INFILE is not a file." fi exit $RET
#!/bin/sh if [ $# -lt 2 ] ; then echo "ERROR: Insufficient arguments." ; exit 1 ; fi case "$1" in -o) printf "%o\n" "$2" ;; -x) printf "%x\n" "$2" ;; -e) printf "%e\n" "$2" ;; *) echo "ERROR: Unknown conversion, $1!" ;; esac
#!/bin/sh if [ $# -lt 2 ] ; then echo "ERROR: Insufficient arguments." >&2 exit 1 ; fi case "$1" in -o) printf "%o\n" "$2" ;; -x) printf "%x\n" "$2" ;; -e) printf "%e\n" "$2" ;; *) echo "ERROR: Unknown conversion, $1!" >&2 ;; esac
mymkdir() { if [ $# -lt 1 ] ; then echo "ERROR: Insufficient arguments." >&2 return 1 fi mkdir -p "$1" > /dev/null 2>&1 if [ $? -eq 0 ] ; then cd "$1" > /dev/null 2>&1 if [ $? -eq 0 ] ; then pwd ; else echo "ERROR: Could not cd to $1." >&2 fi else echo "ERROR: Could not mkdir $1." >&2 fi }
Prompt_RESPONSE() { if [ $# -lt 1 ] ; then echo "ERROR: Insufficient arguments." >&2 return 1 fi RESPONSE= while [ -z "$RESPONSE" ] do echo "$1 \c " read RESPONSE done export RESPONSE }
lspids() { USAGE="Usage: lspids [-h] process" HEADER=false PSCMD="/bin/ps -ef" case "$1" in -h) HEADER=true ; shift ;; esac if [ -z "$1" ] ; then echo $USAGE ; return 1 ; fi if [ "$HEADER" = "true" ] ; then $PSCMD 2> /dev/null | head -n 1 ; fi $PSCMD 2> /dev/null | grep "$1"| grep -v grep }
PSCMD="/bin/ps -ef"
PSCMD="/bin/ps -auwx"
lspids () { USAGE="Usage: lspids [-h|-s] process"; HEADER=false; SORT=false; PSCMD="/bin/ps -ef"; SORTCMD="sort -rn -k 2,2"; for OPT in $@; do case "$OPT" in -h) HEADER=true; shift ;; -s) SORT=true; shift ;; -*) echo $USAGE; return 1 ;; esac; done; if [ -z "$1" ]; then echo $USAGE; return 1; fi; if [ "$HEADER" = "true" ]; then $PSCMD | head -1; fi; if [ "$SORT" = "true" ]; then $PSCMD 2> /dev/null | grep "$1" | grep -v grep | $SORTCMD; else $PSCMD 2> /dev/null | grep "$1" | grep -v grep; fi }
SORTCMD="sort -rn"
SORTCMD="sort -rn -k 2,2"
PSCMD="/bin/ps -ef"
PSCMD="/bin/ps -auwx"
sgrep() { if [ $# -lt 2 ] ; then echo "USAGE: sgrep pattern files" >&2 exit 1 fi PAT="$1" ; shift ; for i in $@ ; do if [ -f "$i" ] ; then sed -n "/$PAT/p" $i else echo "ERROR: $i not a file." >&2 fi done return 0 }
$ uptime | sed 's/.* load/load/'
$ df -k | sed -n '/^\//p' $ df -k | sed '/^[^\/]/d'
/bin/ls -al | sed -e '/^[^\-]/d' -e 's/ *[0-9].* / /'
#!/bin/sh if [ $# -lt 1 ] ; then echo "USAGE: `basename $0` files" exit 1 fi awk '{ for (i=NF;i>=1;i) { printf("%s ",$i) ; } printf("\n") ; }' $@
#!/bin/sh awk 'BEGIN { FS=":" ; } $1 == "B" { BAL=$NF ; next ; } $1 == "D" { BAL += $NF ; } ($1 == "C") || ($1 == "W") { BAL-=$NF ; } ($1 == "C") || ($1 == "W") || ($1 == "D") { printf "%10-s %8.2f\n",$2,BAL ; } ' account.txt ;
#!/bin/sh awk -F: ' $1 == "B" { BAL=$NF ; next ; } $1 == "D" { BAL += $NF ; } ($1 == "C") || ($1 == "W") { BAL-=$NF ; } ($1 == "C") || ($1 == "W") || ($1 == "D") { printf "%10-s %8.2f\n",$2,BAL ; } ' account.txt ;
#!/bin/sh awk -F: ' $1 == "B" { BAL=$NF ; next ; } $1 == "D" { BAL += $NF ; } ($1 == "C") || ($1 == "W") { BAL-=$NF ; } ($1 == "C") || ($1 == "W") || ($1 == "D") { printf "%10-s %8.2f\n",$2,BAL ; } END { printf "-\n%10-s %8.2f\n","Total",BAL ; } ' account.txt ;
#!/bin/sh awk -F: ' $1 == "B" { BAL=$NF ; next ; } $1 == "M" { MIN=$NF ; next ; } $1 == "D" { BAL += $NF ; } ($1 == "C") || ($1 == "W") { BAL-=$NF ; } ($1 == "C") || ($1 == "W") || ($1 == "D") { printf "%10-s %8.2f",$2,BAL ; if ( BAL < MIN ) { printf " * Below Min. Balance" } printf "\n" ; } END { printf "-\n%10-s %8.2f\n","Total",BAL ; } ' account.txt ;
$ type process2
$ find /data -name '*process2*' -print
PRICE=`echo "scale=2; 3.5 \* $PRICE" | bc`
trap CleanUp 2 15 trap Init 1 trap "quit=true" 3 PROG="$1" Init while : ; do wait $! if [ "$quit" = true ] ; then exit 0 ; fi $PROG & done
#! /bin/sh AlarmHandler() { echo "Got SIGALARM, cmd took too long." KillSubProcs exit 14 } IntHandler() { echo "Got SIGINT, user interrupt." KillSubProcs exit 2 } KillSubProcs() { kill ${CHPROCIDS:-$!} if [ $? -eq 0 ] ; then echo "Sub-processes killed." ; fi } SetTimer() { DEF_TOUT=${1:-10}; if [ $DEF_TOUT -ne 0 ] ; then sleep $DEF_TOUT && kill -s 14 $$ & CHPROCIDS="$CHPROCIDS $!" TIMERPROC=$! fi } UnsetTimer() { kill $TIMERPROC } # main() trap AlarmHandler 14 trap IntHandler 2 SetTimer 15 $PROG & CHPROCIDS="$CHPROCIDS $!" wait $! UnsetTimer echo "All Done." exit 0
$ /bin/sh option script arg1 arg2 arg3
#!/bin/sh option
set option
Debug() { if [ "$DEBUG" = "true" ] ; then if [ "$1" = "on" -o "$1" = "ON" ] ; then set -x else set +x echo " >Press Enter To Continue< \c" read press_enter_to_continue fi fi }
################################################ # Name: toLower # Desc: changes an input string to lower case # Args: $@ -> string to change ################################################ toLower() { echo $@ | tr '[A-Z]' '[a-z]' ; }
################################################ # Name: toUpper # Desc: changes an input string to upper case # Args: $@ -> string to change ################################################ toUpper() { echo $@ | tr '[a-z]' '[A-Z]' }
################################################ # Name: isSpaceAvailable # Desc: returns true (0) if space available # Args: $1 -> The directory to check # $2 -> The amount of space to check for ################################################ isSpaceAvailable() { if [ $# -lt 2 ] ; then printERROR "Insufficient Arguments." return 1 fi if [ ! -d "$1" ] ; then printERROR "$1 is not a directory." return 1 fi if [ `getSpaceFree "$1"` -gt "$2" ] ; then return 0 fi return 1 }
################################################ # Name: isSpaceAvailable # Desc: returns true (0) if space available # Args: $1 -> The directory to check # $2 -> The amount of space to check for # $3 -> The units for $2 (optional) # k for kilobytes # m for megabytes # g for gigabytes ################################################ isSpaceAvailable() { if [ $# -lt 2 ] ; then printERROR "Insufficient Arguments." return 1 fi if [ ! -d "$1" ] ; then printERROR "$1 is not a directory." return 1 fi SPACE_MIN="$2" case "$3" in [mM]|[mM][bB]) SPACE_MIN=`echo "$SPACE_MIN * 1024" | bc` ;; Γ|Γ[bB]) SPACE_MIN=`echo "$SPACE_MIN * 1024 * 1024" | bc` ;; esac if [ `getSpaceFree "$1"`1 -gt "$SPACE_MIN" ] ; then return 0 fi return 1 }
################################################ # Name: isUserRoot # Desc: returns true (0) if the users UID=0 # Args: $1 -> a user name (optional) ################################################ isUserRoot() { if [ "`getUID $1`" -eq 0 ] ; then return 0 fi return 1 }
# initalize the destination directory DESTDIR="$2"; # check if the destination exits if [ ! -d "$DESTDIR" ] ; then # if the destination doesn't exist then assume the destination is # the new name for the directory DESTDIR="`/usr/bin/dirname $2`" NEWNAME="`'/bin/basename $2`" fi # if dirname returns a relative dir we will be confused after cd'ing # latter on. So reset it to the full path. DESTDIR=`(cd $DESTDIR ; pwd ; )` # if the parent of the destination doesn't exist, # were in trouble. Tell the user and exit. if [ ! -d "$DESTDIR" ] ; then printERROR "A parent of the destination directory $2 does not exist" fi
55 grep "$1" "$TMPF1" > "$TMPF2" 2> /dev/null 56 Failed $? "No matches found."
55 sed -n "/^$1[^:]*:/p" "$TMPF1" > "$TMPF2" 2> /dev/null 56 test -s "$TMPF2" > /dev/null 57 Failed $? "No matches found."
79 grep -v "$LINE" "$TMPF1" > "$TMPF1.new" 2> /dev/null
sed -e "s/^$LINE$//" "$TMPF1" > "$TMPF1.new" 2> /dev/null
trap 'echo "Cleaning Up." ; doCleanUp ; exit 2; ' 2 3 15
cp "$MYADDRESSBOOK" "$TMPF1" 2> /dev/null
getCharCount() { case 'getOSName' in bsd|sunos|linux) WCOPT="-c" ;; *) WCOPT="-m" ;; esac wc $WCOPT $@ }
Table of Contents |