#File:ShCheatSheet.sh v.1.1.0 docs at the end
#source:http://www.dartmouth.edu/~rc/classes/ksh/index.html#for loop example
for file in * ; do echo Processing $file ; done ;
for file in `ls -1` ; do echo Processing $file ; done ;
#if example
if [ -r $myfile ]
then
cat $myfile
else
echo $myfile not readable
fi
#case example
case $filename in
*.dat)
echo Processing a .dat file
;;
*.sas)
echo Processing a .sas file
;;
*)
# catch anything else that doesn't match patterns
echo "Don't know how to deal with $filename"
;;
esac
*
Matches any zero or more characters.
?
Matches any one character.
[string]
Matches exactly one character that is a member of the string string. This is called a character class. As a shorthand, string may contain ranges, which consist of two characters with a dash between them. For example, the class ‘[a-z0-9_]’ matches a lowercase letter, a number, or an underscore. You can negate a class by placing a ‘!’ or ‘^’ immediately after the opening bracket. Thus, ‘[^A-Z@]’ matches any character except an uppercase letter or an at sign.
\
Removes the special meaning of the character that follows it. This works even in character classes.
# while example
count=0
max=10
while [[ $count -lt $max ]] ; do
echo $count ;
count=$((count + 1)) ;
done ;
echo "Value of count after loop is: $count"
wlog(){
echo "`date +%Y.%m.%d-%H:%M:%S` [$$] $*" >> $LOGFILE
test -t 1 && echo "$*"
}
send_mail(){
# $1 = subject
# $* = recipients
# - = text
test -z "$2" && {
wlog "NOTICE: no mail recipients"
return 0
}
_sm_s="$1"
shift 1
mailx -s "$_sm_s" $* || {
wlog "ERROR: Failed to send mail to $*"
return 1
}
return 0
}
#get a nice date
`date +%Y%m%d%H%M%S`
outgoing_dirs=`find $ROOTDIR -type d -name 'outgoing'`
for odir in $outgoing_dirs
do
test -d "$odir" || continue
find $odir -type f -mtime $MAXAGE -print0 | xargs --null -r $debug rm
done
#The ultimate find in files
DirToSearch=/var/log/
FilesToSearch=`ls -rt $DirToSearch`
for file in $FilesToSearch
do
file=$DirToSearch/$file
test -f "$file" || continue
grep -nH 'sekauppi' $file
done
find $DirToSearch -type f -exec grep -nH 'yogeorgi' {} \; | less
today=`date +%Y%m%d`
yesterday=`date --date yesterday +%Y%m%d`
Help(){
cat<<EOF
`basename $0` [-t filetype] [-f srcfile] [-o outfile] [-D outdir]
[-u owner] [-g group] [-U srcurl] [-d day -d day2 ...]
[-F] [-b daysback]
filetype: {userlist | auditlog}
user: $channel_user
srcurl: $BASE_URL
daysback: $DAYS_BACK
-F to force download even if file was downloaded already
EOF
#Purpose:
# Provide an easy copy paste resource for sh programming
# VersionHistory:
# 1.1.0 --- ysg --- Updated docs , published
# 1.0.0 --- ysg --- Initial version , see main source
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!