some unix shell snippet
时间:2007-07-26 来源:junsi
If you are a c programer, below shell code may be useful.
but it isn't test carefully, so any sugguestion, comments, bugs fix are
welcome. :-)
# uu shell snippet
# junsi 2007 in shanghai
# version 0.01
uu_strlen()
{
expr length $1
} uu_strstr()
{
expr index $1 $2
} uu_strcpy()
{
echo "$1$2"
} uu_substr()
{
expr substr "$1" $2 $3
} uu_split_n()
{
echo $1 | awk -f"$2" ' print $'"$3"''
} uu_replace()
{
echo $1 | sed 's/'"$1"'/'"$2"'/g'
} uu_to_upper()
{
echo "$1" | tr '[a-z]' '[A-Z]'
#awk 'BEGIN{ print toupper('"$1"') }'
}
uu_to_lower()
{
echo "$1" | tr '[A-Z]' '[a-z]'
#awk 'BEGIN{ print tolower('"$1"') }'
} uu_strim()
{
echo $1 | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'
} uu_strim_left()
{
echo $1 | sed 's/^[ ]*//g'
} uu_strim_right()
{
echo $1 | sed 's/[ ]*$//g'
}
uu_pathname()
{
dirname $1
} uu_filename()
{
basename $1
} uu_date()
{
date +"%Y%m%d"
} uu_time()
{
date +"%H%M%S"
} uu_date_before()
{
perl -e '@tptime=localtime(time()-'"$1"'*86400); print @tptime[5]+1900, @tptime[4]+1, @tptime[3]'
} uu_date_after()
{
perl -e '@tptime=localtime(time()+'"$1"'*86400); print @tptime[5]+1900, @tptime[4]+1, @tptime[3]'
} uu_date_to_stamp()
{
perl -e 'print time()'
} uu_stamp_to_date()
{
perl -e '@tptime=localtime('"$1"'); print @tptime[5]+1900, @tptime[4]+1, @tptime[3]'
} uu_process_exist()
{
ps -ef | grep $1 | wc -l
} uu_kill_process()
{
for pp in `ps -ef | grep $1 | grep -v grep | awk '{ print $2 }'`
do
kill -9 $pp
done
} uu_java_path()
{
jpath=(cd $1; pwd)
find $jpath -name \*.jar -print | paste -s -d: -
} uu_rm_files_byname()
{
find $1 -name $2 -exec rm -f {} \;
} uu_rm_files_bytime()
{
find $1 -atime +"$2" -exec rm -f {} \;
}
{
expr length $1
} uu_strstr()
{
expr index $1 $2
} uu_strcpy()
{
echo "$1$2"
} uu_substr()
{
expr substr "$1" $2 $3
} uu_split_n()
{
echo $1 | awk -f"$2" ' print $'"$3"''
} uu_replace()
{
echo $1 | sed 's/'"$1"'/'"$2"'/g'
} uu_to_upper()
{
echo "$1" | tr '[a-z]' '[A-Z]'
#awk 'BEGIN{ print toupper('"$1"') }'
}
uu_to_lower()
{
echo "$1" | tr '[A-Z]' '[a-z]'
#awk 'BEGIN{ print tolower('"$1"') }'
} uu_strim()
{
echo $1 | sed 's/^[ ]*//g' | sed 's/[ ]*$//g'
} uu_strim_left()
{
echo $1 | sed 's/^[ ]*//g'
} uu_strim_right()
{
echo $1 | sed 's/[ ]*$//g'
}
uu_pathname()
{
dirname $1
} uu_filename()
{
basename $1
} uu_date()
{
date +"%Y%m%d"
} uu_time()
{
date +"%H%M%S"
} uu_date_before()
{
perl -e '@tptime=localtime(time()-'"$1"'*86400); print @tptime[5]+1900, @tptime[4]+1, @tptime[3]'
} uu_date_after()
{
perl -e '@tptime=localtime(time()+'"$1"'*86400); print @tptime[5]+1900, @tptime[4]+1, @tptime[3]'
} uu_date_to_stamp()
{
perl -e 'print time()'
} uu_stamp_to_date()
{
perl -e '@tptime=localtime('"$1"'); print @tptime[5]+1900, @tptime[4]+1, @tptime[3]'
} uu_process_exist()
{
ps -ef | grep $1 | wc -l
} uu_kill_process()
{
for pp in `ps -ef | grep $1 | grep -v grep | awk '{ print $2 }'`
do
kill -9 $pp
done
} uu_java_path()
{
jpath=(cd $1; pwd)
find $jpath -name \*.jar -print | paste -s -d: -
} uu_rm_files_byname()
{
find $1 -name $2 -exec rm -f {} \;
} uu_rm_files_bytime()
{
find $1 -atime +"$2" -exec rm -f {} \;
}
相关阅读 更多 +