--**GURUNANAK ** 11 June 2011 ** ** ** --********************************** #!/bin/ksh ################################################### # Written By: Jason Thomas # Purpose: This script was written to show users how to develop their first script # May 1, 2008 ################################################### #Define Variables HOME="/home/jthomas" #Simple home directory DATE=$(date) # Set DATE equal to the output of running the shell command date HOSTNAME=$(hostname) # Set HOSTNAME equal to the output of the hostname command PASSWORD_FILE="/etc/passwd" # Set AIX password file path #Begin Code for username in $(cat $PASSWORD_FILE | cut -f1 -d:) do print $username done #Begin Code PASSWORD_FILE="/etc/passwd" ls –l $PASSWORD_FILE > /dev/null 2>&1 if [[ $? != 0 ]]; then print “$PASSWORD_FILE was not found" exit else for username in $(cat $PASSWORD_FILE | cut -f1 -d:) do print $username done fi --**SENDMail #!/usr/bin/ksh export MAILTO="spam@ebay.com" export SUBJECT="Report" export BODY="/tmp/report.html" export ATTACH="/tmp/sample.pdf" ( echo "To: $MAILTO" echo "Subject: $SUBJECT" echo "MIME-Version: 1.0" echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"' echo echo '---q1w2e3r4t5' echo "Content-Type: text/html" echo "Content-Disposition: inline" cat $BODY echo '---q1w2e3r4t5' echo 'Content-Type: application; name="'$(basename $ATTACH)'"' echo "Content-Transfer-Encoding: base64" echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"' uuencode --base64 $ATTACH $(basename $ATTACH) echo '---q1w2e3r4t5--' ) | /usr/sbin/sendmail $MAILTO --**DOUBLERUN ----prevent double run of script #! /bin/ksh typeset -i iProcNr="$$" # our own process nr typeset fPIDFile="/some/procfile.pid" if [ -f "$fPIDFile" ] ; then print -u2 "ERROR: script already running, exiting." exit 1 else print - "$iProcNr" > "$fPIDFile" fi .... here goes the rest of your code .... rm -f "$fPIDFile" exit 0 --**substring #!/usr/bin/bash name="Leonardo" longitud=`expr length "$name"` for i in $(seq 0 $longitud); do echo ${name:$i:1} done --**AIX io metrics vmstat -Itw 2 4 iostat -DlT 2 4 --**DATE --**Timestamp --**TIME date "+%D" date "+%Y-%m-%d %H:%M:%S" date "+%F %T" date "+%T" eval "$(date "+year=%Y month=%m day=%d hour=%H minute=%M second=%S")" --**scriptname scriptname=${0##*/} --**scriptpath dir = $(dirname $0) ---- if [[ $0 != "/"* ]]; then DIR=`pwd`/`dirname $0` else DIR=`dirname $0` fi ---- __REAL_SCRIPTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P ) --**Links cd "$HOME/bin" && for name in sleepy sneezy grumpy dopey do ln -s bashful "$name" ## you can leave out the -s option if you like done --**ARRAY load from file IFS=$'\n' ## split on newlines, so each line is a separate element array=( $(cat "$kjv") ) --**CURSOR turn off ## Turn off cursor printf "%s" "${CSI}?25l" ## Turn on printf "%s" "${CSI}0m" --**scriptpath dir = $(dirname $0) CURPATH=$( cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P ) if [[ $0 != "/"* ]]; then DIR=`pwd`/`dirname $0` else DIR=`dirname $0` fi --**echo off input echo "enter something \c" stty -echo read something stty echo --**case case "$field" in "[tT][eE][sS][tT]) action1;; *) action2;; esac --**uppercase tr "[:lower:]" "[:upper:]" < file echo "my lowercase string" | tr "[:lower:]" "[:upper:]" echo "my lowercase string" | awk '{print toupper($0)}' #!/bin/ksh ############ Using exec to do I/O on multiple files ############ USAGE="usage : ex4.ksh file1 file2" if (($# != 2)) # this script needs 2 arguments then print "$USAGE" exit 1 fi ############ Both arguments must be readable regular files ##### if [[ (-f $1) && (-f $2) && (-r $1) && (-r $2) ]] then # use exec to open 4 files exec 3 <$1 # open $1 for input exec 4 <$2 # open $2 for input exec 5> match # open file "match" for output exec 6> nomatch # open file "nomatch" for output else # if user enters bad arguments print "$USAGE" exit 2 fi while read -u3 lineA # read a line on descriptor 3 do read -u4 lineB # read a line on descriptor 4 if [ "$lineA" = "$lineB" ] then # send matching line to one file print -u5 "$lineA" else # send nonmatching lines to another print -u6 "$lineA; $lineB" fi done print "Done, today : $(date)" # $(date) : output of 'date' command date_var=$(date) # or put it in a variable print " I said $date_var" # and print it... --**menu echo "--------------- Menu select ----------" PS3="Enter your choice: " select menu_list in English francais quit do case $menu_list in English) print "Thank you";; francais) print "Merci.";; quit) break;; *) print " ????";; esac done --**menu #!/bin/ksh trap "" 2 if [ `/bin/who |/bin/grep "^$LOGNAME " |/bin/wc -l |/usr/bin/awk ' {print $1}'` -lt "2" ] then ulimit -c 2 while [ 1 ] do tput clear echo "" echo " ********************************************" echo " Promotion Administrator Menu" echo " ********************************************" echo "" echo " 1. Prod Menu Screen" echo "" echo " 2. Test Menu Screen" echo "" echo " 3. Change Password" echo "" echo " 4. System Messages" echo "" echo " 5. Exit" echo "" echo " Enter Choice : \c" read choice case $choice in 1) /usr/bin/goprod;; 2) /usr/bin/gotest ;; 3) /usr/bin/passch ;; 4) /usr/bin/sysmsg.sh ;; 5) tput clear echo " *******************************************" echo " Exit from System Completed." echo " *******************************************" exit ;; *) echo "\n Invalid Option" ;; esac done else echo "Too Many Login Attempts." /usr/local/bin/excesslogins.sh echo "Pausing 10 seconds before closing window" sleep 10 exit fi --**readchar --**getchar keybd() { key="${.sh.edchar}" } trap 'keybd' KEYBD while : do read a done ---- trap ':' HUP INT QUIT c="" while [ "$c" = "" ] do # do c read, look previous dd or echo -n "key:" read -n 1 c # test it case "$c" in \001) ;; # ctrl-A \003) ;; # ctrl-C esac # remove char ascii 01-04 c=$(echo "$c" | tr -d "[\001-\004]" ) done --**awk awk ’ {for (i = 1; i<=NF ; i++) num[$i]++ } END {for (word in num) print word, num[word] } ’ $* ----length line longest awk ' { if ( length > L ) { L=length} }END{ print L}' ----length field longest awk '!len || length($1) > len {len=length($1);s=$1} END{print s, len}' --**Screen Size cols=$(tput cols) rows=$(tput lines) stty size | read rows cols --**Locate --**gotoxy tput cup 5 15 --**string length expr length STRING ${#myVar} echo nixcraft | awk ‘ { print length } ‘ --**string of chars N=100 myvar=`seq 1 $N | sed 's/.*/./' | tr -d '\n'` head -c 16 < /dev/zero | tr '\0' '*' function myPrint { for i in `seq 1 $2`; do echo -n $1; done } myPrint myvar=$(for i in {1..100};do printf "%s" "#";done;printf "\n") --**Arrow Keys # Antonio Macchi has a simpler alternative. #!/bin/bash while true do read -sn1 a test "$a" == `echo -en "\e"` || continue read -sn1 a test "$a" == "[" || continue read -sn1 a case "$a" in A) echo "up";; B) echo "down";; C) echo "right";; D) echo "left";; esac done --**keys key() { local kp ESC=$'\e' _KEY= read -d '' -sn1 _KEY case $_KEY in "$ESC") while read -d '' -sn1 -t1 kp do _KEY=$_KEY$kp case $kp in [a-zA-NP-Z~]) break;; esac done ;; esac printf -v "${1:-_KEY}" "%s" "$_KEY" } _key x case $x in $'\e[15~') echo You have pressed F5 ;; esac case $x in $'\e[11~' | $'\e[OP') key=F1 ;; $'\e[12~' | $'\e[OQ') key=F2 ;; $'\e[13~' | $'\e[OR') key=F3 ;; $'\e[14~' | $'\e[OS') key=F4 ;; $'\e[15~') key=F5 ;; $'\e[16~') key=F6 ;; $'\e[17~') key=F7 ;; $'\e[18~') key=F8 ;; $'\e[19~') key=F9 ;; $'\e[20~') key=F10 ;; $'\e[21~') key=F11 ;; $'\e[22~') key=F12 ;; $'\e[A' ) key=UP ;; $'\e[B' ) key=DOWN ;; $'\e[C' ) key=RIGHT ;; $'\e[D' ) key=LEFT ;; ?) key=$x ;; *) key=??? ;; esac echo "You have pressed $key" --**mailx #!/bin/sh # # Purpose: Demonstrate how to send an email from UNIX using mailx. ############################################################ # Example 1 - Simple: echo "This is the body."| mailx -s "mailx Test1" jsmith@abc.com # Example 2 - Using Variables: SUBJECT="mailx Test2" EMAIL_ADDRESS="jsmith@abc.com" BODY="This is the body of the message." echo "$BODY" | mailx -s "$SUBJECT" "$EMAIL_ADDRESS" # Example 3 - Attached File: SUBJECT="mailx Test3" EMAIL_ADDRESS="jsmith@abc.com" BODY="This is the body of the message." ATTACHED_FILE="/etc/hosts" cat "$ATTACHED_FILE" | mailx -s "$SUBJECT" "$EMAIL_ADDRESS" # Example 4 - Attached File to Windows Email: SUBJECT="mailx Test4" EMAIL_ADDRESS="jsmith@abc.com" BODY="This is the body of the message." ATTACHED_FILE_DIR="/etc/" ATTACHED_FILE="hosts" ATTACHED_FILE_AS="hosts.txt" unix2dos "$ATTACHED_FILE_DIR$ATTACHED_FILE" > /tmp/"$ATTACHED_FILE_AS" uuencode /tmp/"$ATTACHED_FILE_AS" /tmp/"$ATTACHED_FILE_AS" | mailx -s "$SUBJECT" "$EMAIL_ADDRESS" rm /tmp/"$ATTACHED_FILE_AS" --**pause #! /usr/bin/ksh function pause { typeset savesetting result print -n "Hit any key to continue..." savesetting=$(stty -g) stty -icanon min 1 time 0 result=$(dd bs=1 count=1 2>/dev/null) stty "$savesetting" echo } ##==============================================string "" ##--echos the string of so many () characters ("") string() { printf "******************************************\ *************************************************************\ *************************************************************\ **************************" | head -c $2 | tr '*' "$1";} ##============================================== ##------------HP-UX has different head syntax - tail is good instead, or maybe cut ##============================================== string() { printf "kkkkkkkkkkkkkkkkkkkkkkkk" | tail -c $2 | tr 'k' "$1";} string() { print "kkkkkkkkkkkkkkkkkkkkkkkk" | cut -c1-$2 | tr 'k' "$1";} ##--------------------------------------------cut needs EOL --**ASCII string to HEX string hxstr=$* while [[ ${#hxstr} -gt 0 ]]; do printf "${hxstr:0:1}"| od -An -t xC | tr ' ' '\0'| tr '\12' '\0' hxstr=${hxstr:1} done print --**HexString to AscString (array) num=0 while [[ ${#cmd[$num]} -gt 0 ]] do hxstr=${cmd[$num]}; cmd[$num]="" while [[ ${#hxstr} -gt 0 ]]; do cmd[$num]=${cmd[$num]}$(printf "\x${hxstr:0:2}") hxstr=${hxstr:2} done ((num+=1)) done