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

6.脚本定时任务

<span class="hljs-preprocessor"># Example of job definition:</span>
<span class="hljs-preprocessor"># .-------------------------  minute (0 - 59)</span>
<span class="hljs-preprocessor"># |    .---------------------  hour (0 - 23)</span>
<span class="hljs-preprocessor"># |    |    .-----------------  day of month (1 - 31)</span>
<span class="hljs-preprocessor"># |    |    |    .-------------  month (1 - 12) </span>
<span class="hljs-preprocessor"># |    |    |    |    .---------  day of week (0 - 6) </span>
<span class="hljs-preprocessor"># |    |    |    |    |</span>
<span class="hljs-preprocessor"># *    *    *    *    *   user-name   command to be executed</span>

7.查看当前用户的UID

root<span class="hljs-variable">@kallen</span><span class="hljs-symbol">:/usr/data/kallendb_backup</span>
<span class="hljs-constant">UID</span> <span class="hljs-constant">PID</span> <span class="hljs-constant">PPID</span> <span class="hljs-constant">C</span> <span class="hljs-constant">STIME</span> <span class="hljs-constant">TTY</span> <span class="hljs-constant">TIME</span> <span class="hljs-constant">CMD</span>
root <span class="hljs-number">2872</span> <span class="hljs-number">2384</span> <span class="hljs-number">0</span> 09<span class="hljs-symbol">:</span><span class="hljs-number">43</span> pts/<span class="hljs-number">2</span> <span class="hljs-number">00</span><span class="hljs-symbol">:</span><span class="hljs-number">00</span><span class="hljs-symbol">:</span><span class="hljs-number">00</span> grep --color=auto <span class="hljs-constant">UID</span>

8.用Shell模拟一个进度条


  b=<span class="hljs-string">''</span>

  <span class="hljs-keyword">for</span> ((i=<span class="hljs-number">0</span>;<span class="hljs-variable">$i</span><=<span class="hljs-number">100;i+=<span class="hljs-number">2</span>))
  <span class="hljs-keyword">do</span>
      <span class="hljs-built_in">printf</span> <span class="hljs-string">"Progress:[%-50s]%d%%\r"</span> <span class="hljs-variable">$b</span> <span class="hljs-variable">$i</span>
      sleep <span class="hljs-number">0.1</span>

      b=
  <span class="hljs-keyword">done</span>
  <span class="hljs-built_in">echo</span></=<span>

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

在Shell脚本的编写应用中,有时候会须要用到图形界面的案例,比方默认cp复制文件为静默模式。无法看到拷贝的进度与百分比。
而dialog正是为Shell提供图形界面的工具,该工具能够为Shell脚本提供各式各样的图形界面,今天为大家介绍的是dialog提供的进度条图形功能。

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

 dialog --title <span class="hljs-string">"Copy"</span> --gauge <span class="hljs-string">"files"</span> <span class="hljs-number">6</span> <span class="hljs-number">70</span> <span class="hljs-number">10</span>

备注:

title表示图形进度条的标题。
gauge为正文内容。进度条高度为6,宽度70。显示运行进度为10%

<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> {<span class="hljs-number">1</span>..<span class="hljs-number">100</span>} ;
<span class="hljs-keyword">do</span> sleep <span class="hljs-number">1</span>;
    <span class="hljs-built_in">echo</span> <span class="hljs-variable">$i</span> | dialog --title <span class="hljs-string">'Copy'</span> --gauge <span class="hljs-string">'I am busy!'</span> <span class="hljs-number">10</span> <span class="hljs-number">70</span> <span class="hljs-number">0</span>;
<span class="hljs-keyword">done</span>

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

该脚本有两个參数。第一个參数为源文件路径,第二个參数为目标路径。
假设您的应用案例不同能够据此稍作改动就可以使用。

<span class="hljs-shebang">#!/bin/bash    </span>

dir=<span class="hljs-variable">$1</span>/*
des=<span class="hljs-variable">$2</span>

[ <span class="hljs-operator">-d</span> <span class="hljs-variable">$des</span> ] && <span class="hljs-built_in">echo</span> <span class="hljs-string">"Dir Exist"</span> && <span class="hljs-keyword">exit</span> <span class="hljs-number">1</span>

mkdir <span class="hljs-variable">$des</span>

i=<span class="hljs-number">0</span>

n=<span class="hljs-built_in">echo</span> <span class="hljs-variable">$1</span>/* |wc -w

<span class="hljs-keyword">for</span> file <span class="hljs-keyword">in</span> <span class="hljs-built_in">echo</span> <span class="hljs-variable">$dir</span>
<span class="hljs-keyword">do</span>

    percent=$((<span class="hljs-number">100</span>*(++i)/n))
    cat <<eof xxx <span class="hljs-variable">$percent
    Copying file <span class="hljs-variable">$file</span> ...

    XXX
    EOF
    /bin/cp -r <span class="hljs-variable">$file</span> <span class="hljs-variable">$des</span> &>/dev/null
<span class="hljs-keyword">done</span> | dialog --title <span class="hljs-string">"Copy"</span> --gauge <span class="hljs-string">"files"</span> <span class="hljs-number">6</span> <span class="hljs-number">70</span>
clear</eof>

效果如图:

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

9.Echo输出

功能说明: 显示文字
语 法:

<span class="hljs-tag">echo</span> <span class="hljs-attr_selector">[ -ne ]</span>  <span class="hljs-attr_selector">[ &#x5B57;&#x7B26;&#x4E32; ]</span>  &#x6216;
<span class="hljs-tag">echo</span> <span class="hljs-attr_selector">[ --help ]</span>  <span class="hljs-attr_selector">[--version ]</span>

參数:

-<span class="ruby">n          &#x4E0D;&#x8981;&#x5728;&#x6700;&#x540E;&#x81EA;&#x5DF1;&#x4E3B;&#x52A8;&#x6362;&#x884C;
</span>-<span class="ruby">e          &#x82E5;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x51FA;&#x73B0;&#x4EE5;&#x4E0B;&#x5B57;&#x7B26;&#xFF0C;&#x5219;&#x7279;&#x522B;&#x52A0;&#x4EE5;&#x5904;&#x7406;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x5C06;&#x5B83;&#x5F53;&#x6210;&#x4E00;&#x822C;&#x6587;&#x5B57;&#x8F93;&#x51FA;;
</span>\b          &#x5220;&#x9664;&#x524D;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF1B;
\f          &#x6362;&#x884C;&#x4F46;&#x5149;&#x6807;&#x4ECD;&#x65E7;&#x505C;&#x7559;&#x5728;&#x539F;&#x6765;&#x7684;&#x4F4D;&#x7F6E;&#xFF1B;
\r          &#x5149;&#x6807;&#x79FB;&#x81F3;&#x884C;&#x9996;&#x3002;&#x4F46;&#x4E0D;&#x6362;&#x884C;&#xFF1B;
\t          &#x63D2;&#x5165;tab&#x3002;
\v          &#x4E0E;\f&#x540C;&#x6837;&#xFF1B;
\nnn        &#x63D2;&#x5165;nnn&#xFF08;&#x516B;&#x8FDB;&#x5236;&#xFF09;&#x6240;&#x4EE3;&#x8868;&#x7684;ASCII&#x5B57;&#x7B26;&#x3002;
-<span class="ruby">-help      &#x663E;&#x793A;&#x5E2E;&#x52A9;
</span>-<span class="ruby">-version   &#x663E;&#x793A;&#x7248;&#x672C;&#x53F7;&#x4FE1;&#x606F;</span>

热门推荐

Original: https://www.cnblogs.com/yxwkf/p/5423835.html
Author: yxwkaifa
Title: Shell Step by Step (4) —— Cron &amp; Echo

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/529470/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球