Shell程式設計學習Day01

2021-08-18 02:00:12 字數 2249 閱讀 2334

第乙個shell程式example.sh:

#!/bin/bash

echo "our first example"

echo #empty line

echo "we are currently in the following directory"

pwdecho

echo "this directory contains the following files"

ls

#為單行注釋

#!跟shell命令的完全路徑,顯示後期命令以哪種shell執行

shell程式的建立步驟:

1.建立shell檔案,以.sh結尾。

gedit example.sh
2.修改檔案的許可權 

chmod u+x example.sh
3.執行

方法一:直接執行

./example.sh
方法二:使用bash執行

bash example.sh
方法三:使用絕對路徑執行,在當前目錄下輸入pwd獲得該路徑

/home/hadoop/test/example.sh
執行結果

shell變數分為臨時變數(使用者自定義變數),永久變數(環境變數)

變數賦值,等號左右不要加空格,如

a=aaa

name="li ming"

使用變數時要在變數前面加上$符號

echo $a

echo $name

檢視所有變數輸入:set

單引號和雙引號的區別:單引號之間的內容原封不動的輸出,雙引號取消空格作用,特殊符號的含義保留,如:

刪除變數使用unset

unset a
位置變數位置變數以$n表示,n從0開始,0,1,2,……,如:

./example.sh file1 file2 file3
$0指example.sh,$1指file1,$2指file2,以此類推

特殊變數

$*     程式所有引數

$#    引數個數

$$    程式的pid

$!     執行上乙個後台程式的pid

$?    執行上乙個指令的返回值

舉例:

#!/bin/bash

echo "$* 該程式所有引數"

echo "$# 該程式所有引數個數"

touch /home/hadoop/test/a.txt

echo "$$ 程式的pid"

touch /home/hadoop/test/b.txt &

echo "$! 執行上乙個後台指令的pid"

echo "$$ 程式的pid"

結果:

read命令

從鍵盤中讀取資料

expr命令

做算數運算,乘法需要在*前加轉義 \

舉例:(可以有負數,除不盡的抹去小數部分)

#!/bin/bash

read num1 num2

expr $num1 + $num2

expr $num1 - $num2

expr $num1 \* $num2

expr $num1 / $num2

結果:

Shell自學筆記day 01

定義 shell是乙個命令列形式展現給使用者的應用型程式,連線使用者跟linux核心,給使用者提供更加高效 安全 低成本的方式去操作核心。只知道使用者能通過shell去操作核心還不夠,還得知道shell是如何去操作核心的。用兩字概括就是函式,沒錯,就是函式!而且除了呼叫核心暴露出來的函式介面,沒有其...

學習筆記day01

作業系統簡稱 operating system 簡稱os 其的本質就是乙個軟體,作業系統對上可以管理應用程式,對下可以訪問硬體裝置。主流的作業系統 pc機領域 windows系列,ios,伺服器領域 linux系列,unix,windows server。手持裝置 ios,android,塞班,wi...

前端學習Day01

alt shift 1 非小鍵盤 視窗分屏,恢復預設1屏 alt shift 2 左右分屏 2列 alt shift 3 左右分屏 3列 alt shift 4 左右分屏 4列 alt shift 5 等分4屏 alt shift 8 垂直分屏 2屏 alt shift 9 垂直分屏 3屏 ctrl...