顯示具有 Shell Script 標籤的文章。 顯示所有文章
顯示具有 Shell Script 標籤的文章。 顯示所有文章

2021年1月7日 星期四

Bash 執行 for 1~100 的寫法 (轉貼)

 

Bash 執行 for 1~100 的寫法

最開始只是要對程式跑100次, 做個時間測試而已, 例如下述:

  1. for i in {1..100};
  2. do
  3. time curl -o /dev/null https://blog.longwin.com.tw;
  4. done
  5. # 跑100次, 印出時間

後來想要將變數代入後, 發現一連串的事情.

註: {1..100}, 使用 {1..$END} 無法執行. (需搭配 seq 或者 for 換寫法)

下述就不解釋了~ 直接看程式和結果即可.

  1. for i in {1..5};
  2. do
  3. echo $i;
  4. done
  5. # 印出 1 2 3 4 5 (換行)

要將 {1..5} 的 5 用變數帶入, 就會需要搭配 seq
seq 1 5 # 印出 1 2 3 4 5

  1. END=5
  2. for i in $(seq 1 $END);
  3. do
  4. echo $i;
  5. done
  6. # 印出 1 2 3 4 5 (換行)
  1. # 或者 for 寫法換掉
  2. BEGIN=1
  3. END=5
  4. for ((i=$BEGIN; i<=$END; i++))
  5. do
  6. echo -n "$i "
  7. done
  8. # 用空隔分隔, 印出 1 2 3 4 5
  1. for i in {0..10..2}
  2. do
  3. echo $i
  4. done
  5. # 印出 0 2 4 6 8 10 (換行)
  1. for i in $(seq 1 2 10);
  2. do
  3. echo $i;
  4. done
  5. # 印出 1 3 5 7 9 (換行)

2021年1月6日 星期三

Linux shell script 2>&1 ??? (轉貼)

 當我初次接觸shell script時,看別人寫的script或者範例時, 2>&1這類型的符號真困擾我.

找到了一篇文章,和各位分享 原文 http://bowen.blog.51cto.com/136148/94406

0 代表鍵盤輸入
1 代表螢幕輸出
2 代表錯誤輸出

Ref:

$ cat nop.txt > output.txt
cat: nop.txt: No such file or directory

Even if we redirect the stdout to a file, we still see the error output in the screen, because we are redirecting just the standard output, not the standard error.

Grave accent (`) symbol (轉貼)

 When I type Grave accent(`) symbol in terminal, It is like down below.

>
>
>

What does Grave accent(`) symbol do?

3

The '>' is displayed because the shell 'waits' for the closing quote. From the man page:

Command Substitution
       Command substitution allows the output of a command to replace the com-
       mand name.  There are two forms:
          $(command)
   or
          `command`

Shell脚本中的while getopts用法小结 (轉貼)

Shell脚本中的while getopts用法小结

 

命令格式:

1
getopts optstring name [arg...]

命令描述:
optstring列出了对应的Shell Script可以识别的所有参数。比如:如果 Shell Script可以识别-a,-f以及-s参数,则optstring就是afs;如果对应的参数后面还跟随一个值,则在相应的optstring后面加冒号。比如,a:fs 表示a参数后面会有一个值出现,-a value的形式。另外,getopts执行匹配到a的时候,会把value存放在一个叫OPTARG的Shell Variable当中。如果 optstring是以冒号开头的,命令行当中出现了optstring当中没有的参数将不会提示错误信息。

name表示的是参数的名称,每次执行getopts,会从命令行当中获取下一个参数,然后存放到name当中。如果获取到的参数不在optstring当中列出,则name的值被设置为?。命令行当中的所有参数都有一个index,第一个参数从1开始,依次类推。 另外有一个名为OPTIND的Shell Variable存放下一个要处理的参数的index。

Linux shell script 基本指令 (轉貼)

[Linux][Shell] Linux shell script 基本指令總整理

 

我們也可以把指令的輸出傳回給變數,指令前後加上關鍵字「`」,

它是位於鍵盤左上角「數字鍵1」左邊的按鍵:
now=`date` 
echo now
結果會秀出「Mon Dec 16 09:31:44 EAT 2013」

此外,雙引號與單引號字串對變數解釋有不同的意義,
如果是使用單引號,則變數會當成一般文字;雙引號才會解譯成變數,例如:
a=123
echo "$a" ->結果是123
echo '$a' ->結果是$a

2021年1月5日 星期二

Linux Shell Script 保留/系統預設的變數 (轉貼)

 

保留/系統預設的變數

系統預設已經有相當多的變數定義了, 因此在你的shell script裡面 要去避免這些變數. 以下就是一些預設的系統變數. 

$BASH_ENV absolute path of startup file
$CDPATH directories searched by cd
$FCEDIT absolute path of history editor
$HISTCMD the history number of the current command
$HISFILE absolute path of history file
$HISTSIZE number of remembered commands
$HOME login directory
$IFS token delimiters
$LINENO current line number in shell script
$LINES terminal height
$MAIL  absolute path of mailbox
$MAILCHECK number of seconds to check mail
$OLDPWD absolute path of previous directory
$OPTARG option set by getopt
$OPTIND option's ordinal position set by getopt
$OSTYPE the OS on which bash is executing
$PATH command search path
$PPID  process ID of parent
$PS1  primary prompt
$PS2 secondary prompt
$PWD absolute path of current directory
$RANDOM random integer
$REPLY default variable for read
$SECONDS number of seconds since shell started
$SHELL absolute pathname of preferred shell
$TMOUT seconds to log out after lack of use
$UID user ID of the current user
$$ process ID of current shell
$? exit status of most recent statement
$# 參數的數目
$* 代表所有參數
$! PID of the most recently started backgroup job


另外, 還有參數的傳遞也需要了解, 例如當我們編寫一個 script 內容如下:

#!/bin/bash
echo "\$0 ==> $0"
echo "\$1 ==> $1"
echo "\$2 ==> $2"

則執行以下命令的時候:

./myscript Hello World
$0 ==> ./myscript
$1 ==> Hello
$2 ==> World


You can use $(!!) to recompute (not re-use) the output of the last command.

The !! on its own executes the last command.

$ echo pierre
pierre
$ echo my name is $(!!)
echo my name is $(echo pierre)
my name is pierre