centos中的shell程式設計

2022-05-12 15:42:55 字數 1572 閱讀 2144

1.shell 是批處理程式,類似與windows的bat檔案

2.寫shell時,第一行要以#!/bin/bash 開頭 execute the file using the bash shell.

3.使用#注釋(最好備註shell指令碼的功能作用以防日後忘記)

4.檔名應以.sh結尾

5.執行時,執行方式 sh 1.sh;chmod +x 1.sh; ./1.sh || /root/test/1.sh(絕對路徑)

6.$? //命令的返回值儲存變數

$# //引數個數

$1 //第幾個引數。提取引數

$0 //當前指令碼命令的名稱

$@ //取出所有引數

$shift //引數左移

7.1)建立第乙個指令碼檔案,

$>touch a.sh;//建立指令碼檔案,建立指令碼檔案之後修改檔案許可權,所有人都可以執行該檔案,chmod a+x a.sh

$>#!/bin/bash

echo hello world

這個程式就會列印除hello world

7.2)#!/bin/bash

num=$#

echo num >>1.txt //這個指令碼列印出輸入引數的個數

7.3)#!/bin/bash

echo helloworld!

echo parameters is $#!

echo script's name is $0.

7.4)

#!/bin/bash

echo $1.

shift.

echo $1.

shift.

echo $1.

shift.

echo $1.

shift.

8.if[$# -lt 1] //這句話的意思是如果引數個數小於1

if[$# -gt 1] //這句話的意思是如果引數個數大於1

if commands; then commands; [ elif commands; then commands; ]... [ else commands; ] fi

for ((: for (( exp1; exp2; exp3 )); do commands; done

8.1)#!/bin/bash

num= $@ 取出所有引數

for(( i = 1 ; i <= num ; i = $i+1)) ;do

for((y = 1 ; y <= x ; x= $x+1 )); do

echo -n $y;

done

echo ;

done

8.2)九九乘法表

#!/bin/bash

i=1line=$1

while(( i<= $line )) ; do

j=1while(( j<$i )) ; do

echo -ne $x$=$(( j*i))'\t';

j=$(( j+1 ));

done ;

i=$(( i+1 ))

echo ;

done;

Centos 7 4 1 shell程式設計

linux shell 簡介 從程式設計師的角度來看,shell 本身是一種用c語言編寫的程式,從使用者的角度來看,shell 是使用者與linux作業系統溝通的橋梁。使用者可以通過輸入一條條命令,完成指定工作,也可以通過提前編寫 shell 指令碼,完成批量命令的一次執行。深入了解和熟練掌握she...

Shell程式設計中的函式

一段可以重複使用的指令碼 提前已經編寫好,使用時直接調取。function name 簡化定義 name 和其他程式語言不同的是,shell函式定義時不能指明引數,但是呼叫時可以接受引數,傳給什麼引數,就接受什麼引數。函式引數是位置引數的一種,在函式內部使用 n來接受,如 1表示第乙個引數,2表示第...

Shell程式設計中的if語句

1.語句結構 if condition 當 condition 這個條件成功,也就是退出狀態為0時,才會執行後面的statements,否則不會執行。then statements fi也可以寫成 if condition then 分號一定要寫 statements fi2.示例1 當 if後的命...