Shell Step by Step (4) —— Cron & Echo

时间:2021-05-30 08:19:44

6.脚本定时任务

# Example of job definition:
# .------------------------- minute (0 - 59)
# | .--------------------- hour (0 - 23)
# | | .----------------- day of month (1 - 31)
# | | | .------------- month (1 - 12)
# | | | | .--------- day of week (0 - 6)
# | | | | |
# * * * * * user-name command to be executed

7.查看当前用户的UID

root@kallen:/usr/data/kallendb_backup# ps -ef | grep UID
UID PID PPID C STIME TTY TIME CMD
root 2872 2384 0 09:43 pts/2 00:00:00 grep --color=auto UID

8.用Shell模拟一个进度条

  #! /bin/bash
#
# Progress Bar
# Print # to view the process bar # create variable
b='' # for loop
for ((i=0;$i<=100;i+=2))
do
printf "Progress:[%-50s]%d%%\r" $b $i
sleep 0.1 b=#$b
done
echo

Shell Step by Step (4) —— Cron &amp; Echo

在Shell脚本的编写应用中,有时候会须要用到图形界面的案例,比方默认cp复制文件为静默模式。无法看到拷贝的进度与百分比。

而dialog正是为Shell提供图形界面的工具,该工具能够为Shell脚本提供各式各样的图形界面,今天为大家介绍的是dialog提供的进度条图形功能。

dialog指令能够单独运行。格式为

 dialog --title "Copy" --gauge "files" 6 70 10

备注:

title表示图形进度条的标题。

gauge为正文内容。进度条高度为6,宽度70。显示运行进度为10%

for i in {1..100} ;
do sleep 1;
echo $i | dialog --title 'Copy' --gauge 'I am busy!' 10 70 0;
done

以下案例中通过统计源文件个数。再据此计算出复制文件的百分比,在Shell中提供进度的显示。

该脚本有两个參数。第一个參数为源文件路径,第二个參数为目标路径。

假设您的应用案例不同能够据此稍作改动就可以使用。

#!/bin/bash
# Description: A shell script to copy parameter1 to
# parameter2 and Display a progress bar
# Author:Jacob
# Version:0.1 beta # Read the parameter for copy,$1 is source dir
# and $2 is destination dir.
dir=$1/*
des=$2
# Test the destination dirctory whether exists
[ -d $des ] && echo "Dir Exist" && exit 1
# Create the destination dirctory
mkdir $des
# Set counter, it will auto increase to the number of
# source file.
i=0
# Count the number of source file
n=`echo $1/* |wc -w` for file in `echo $dir`
do
# Calculate progress
percent=$((100*(++i)/n))
cat <<EOF
XXX
$percent
Copying file $file ...
XXX
EOF
/bin/cp -r $file $des &>/dev/null
done | dialog --title "Copy" --gauge "files" 6 70
clear

效果如图:

Shell Step by Step (4) —— Cron &amp; Echo

9.Echo输出

功能说明: 显示文字

语 法:

echo [ -ne ]  [ 字符串 ]  或
echo [ --help ] [--version ]

參数:

-n          不要在最后自己主动换行
-e 若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出;
\b 删除前一个字符;
\f 换行但光标仍旧停留在原来的位置;
\r 光标移至行首。但不换行;
\t 插入tab。
\v 与\f同样;
\nnn 插入nnn(八进制)所代表的ASCII字符。
--help 显示帮助
--version 显示版本号信息

热门推荐