常用shell集合大全

############################################################################################
#                      below fragment code was running fine in RedHat platform
############################################################################################

find ./path -type f -name "file_name"

# 1. find all PDF file of the one week ago under path dir, then move to /home/test dir.
find /path -maxdepth 1 -mtime +7 -name "*.pdf" -exec mv {} /home/test/ \;

# 2. run script between the time1 and time2
date1="6-23:54"
date2=$(date +'%u-%H:%M')
date3="7-06:00"
if [[ $date1 > $date2 || $date2 > $date3 ]]; then
    endTime=`date +%Y5m%d%H%M`
    echo "the condition is match, will exist the program"
    exit
    else
      echo "waiting for next time to execute"
fi

# 3. run the shell script in background
echo "nohup tar --remove-files -zcvf ${folderName}.zip $folderName &" | bash

# 4. zip the file
echo "nohup zip -q -r -m ${dirName}.zip $dirName &" | bash

# 5. echo diff color to display the info in the terminal.
base_dir=$HOME
if [ ! -n "$base_dir" ]; then
    echo -e "\033[41;37m this is error display show. \033[0m"
    echo -e "\033[33m this is warning display show. \033[0m"
    echo -e "\033[32m this is success display show. \033[0m"
fi

# 6. use shell to base64 string
echo "this is test" | base64 -i
echo "" | base64 -d

# 7. convert the file format to unix/linux format
file_full_path=/home/demo/test.sh
dos2unix -k $file_full_path

# 8. use curl to send the http request
json='{"name":"'$name'","age":"'$age'","class":"'$class'"}'
curl $URL -H "Accept: application/json" -H "Content-Type: application/json" -d $json
curl $URL -F "file=@${filePath}/${fileName}" # send the file via curl

# 9. how to combine the obj as array body
obj_arr='['
obj_arr=$obj_arr$json','
len=`expr ${#obj_arr} - 1`
obj_arr=${obj_arr:0:len}']'
echo $obj_arr

# 10. judge the value is same
value=F
if [ "$value" == "F" ]; then
    echo " the value equal F"
    else
      echo "the value is not F"
fi

# 11. use grep to search the words.
text="Hello Word"
file=/home/demo/test/demo.txt
grep -H -n "$text" $file
if [ $? == 0 ]; then # also can use -z parameter to compare, but can not use -n
    echo "found the records"
    else
      echo "not found the such records"
fi

# 12. get the multi match record under the file
grep -H -n -E 'ERROR|ORA-' /home/demo/demo.txt

# 13. convert the string as arr meanwhile split via special char
text='a|b|c|D|e|f|g'
arr_txt=(${text//|/ }) # replace the | as space " "
echo ${arr_txt[@]}
printf "[%s]\n" ${arr_txt[@]}

# 14. get the file count
cd /home/demo/
ls -l | grep "^-" | wc -l

# 15. get the folder count
cd /home/demo/
ls -l | grep "^d" | wc -l

# 16. find the file which exist in 10 for current path
find $PWD -maxdepth 10 ! -type d
for fullFile in `find $PWD -maxdepth 10 ! -type d` ; do
    fullName="${fullFile##*/}" # testDemo.txt
    dir="${fullFile%/*}" # /home/demo
    extension="${fullName##*.}" # .txt
    fileName="${fullName%.*}" # testDemo
    echo $extension | awk '{print toupper(%0)}' # convert the  extension as upcase
    echo $fullName
    echo $dir
    echo $extension
    echo $fileName
done

# 17. shell ignore the case match
shopt -s nocasematch

# 18. judge the string if contains some value
string="/home/demo/test/bin/shell"
if [[ $string =~ "/bin/" ]]; then
    echo "string contains the value /bin/"
fi

# 19. substring the string
echo "abcdefg" | awk '{print substr($1, 2)}' # result: bcdefg
echo "a:b:c:d:e:f" | awk -F ':' '{print $1}' #result: a
echo "a:b:c:d:e:f" | awk -F ':' '{print $2}' #result: b
echo "a:b:c:d:e:f" | awk -F ':' '{print $3}' #result: c

# 20. if the line contains some special chart without split the line.
IFS=$'\n'
txs="this is test txt \n ----> "
echo $txs
unset IFS

# 21. validate the date format is correct or not.
date "+%Y%m%d %H%M" -d "$param1" > /dev/null 2>&1
if [ $? != 0 ]; then
  echo "date format is not correct"
  exit 1
fi

# 22. list file full path with condition file name
find $PWD -maxdepth 10 ! -type d -type f \( -iname "log*.zip" -o -iname "txt*.zip" \)

# 23. if wanna improve the performance can add -m param.
grep -m 2 "${targetContent}" $fullfilepath
zgrep -m 2 "${targetContent}" $fullfilepath

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值