Shell程式設計-建立和執行
1.建立sehll指令碼
一個shell指令碼包含的內容
1)首行 #!/bin/bash
表示使用bash直譯器
注意:第一行的#特殊,表示定義,其他行則表示註釋
2)註釋
除第一行外的其他行,行首加上#即可
3)內容
建立一個shell指令碼
1)建立一個shell檔案
方法很多:
touch
vi/vim
echo "" > 檔名
2)編輯內容:
#!/bin/bash
#this is my first sehll script
echo "hello shell"
echo "hello scala"
3)執行:會發現沒有執行檔案的許可權
[root@VM_0_16_centos es]# ll
total 28
-rw-r--r-- 1 root root 81 May 10 14:14 firstshell
-rw-r--r-- 1 es root 136 May 9 09:28 hello2.txt.tar.gz
-rwxrwxrwx 1 es es 156 May 9 12:38 hello.txt
-rw-rw-r-- 1 es es 136 May 8 19:21 hello.txt.tar.gz
-rw-rw-r-- 1 es es 182 May 8 19:12 hello.txt.zip
drwxr-xr-x 2 es es 4096 May 9 09:32 temp
drwxr-xr-x 2 root root 4096 May 8 19:10 tody
[root@VM_0_16_centos es]# ./firstshell
bash: ./firstshell: Permission denied
[root@VM_0_16_centos es]# chmod +x firstshell
[root@VM_0_16_centos es]# ./firstshell
hello shell
hello scala
2.三種方式執行指令碼:
1)使用相對路徑或者絕對路徑執行
./firstshell
2)bash firstshell
3)source firstshell
4). firstshell
[root@VM_0_16_centos es]# . firstshell
hello shell
hello scala
[root@VM_0_16_centos es]# bash firstshell
hello shell
hello scala
[root@VM_0_16_centos es]# source firstshell
hello shell
hello scala
這麼多方式,有什麼區別呢?
使用. firstshell會生成一個新的bash環境進行執行
使用./ bash . source這幾種方法,會在當前的環境中執行。
例子:
[root@VM_0_16_centos es]# aa=123
[root@VM_0_16_centos es]# echo $aa
123
[root@VM_0_16_centos es]# echo "echo \$aa" > firstshell
[root@VM_0_16_centos es]# cat firstshell
echo $aa
[root@VM_0_16_centos es]# echo "echo \$aa" >> firstshell
[root@VM_0_16_centos es]# cat firstshell
echo $aa
echo $aa
[root@VM_0_16_centos es]# ./firstshell
[root@VM_0_16_centos es]# bash firstshell
[root@VM_0_16_centos es]# source firstshell
123
123
shell變數:
變數:是shell傳遞資料的一種方式,用來代表每個取值的符號名
shell變數設定規則
不能以數字開頭
變數預設都是字串型別
[root@VM_0_16_centos es]# num3=$num+$num2
[root@VM_0_16_centos es]# echo $num3
12+13
如果變數有空格,需要使用單雙引號
[root@VM_0_16_centos es]# pro=pro fillll
-bash: fillll: command not found
[root@VM_0_16_centos es]# pro="pro fillll"
[root@VM_0_16_centos es]#
=號兩邊不能有空格
[root@VM_0_16_centos es]# lxx = 1
-bash: lxx: command not found
[root@VM_0_16_centos es]# lxx =1
-bash: lxx: command not found
3.分類
使用者自定義變數
環境變數 $HOME,$SHELL,$PWD,$USER等
位置引數變數
預定義變數
使用set檢視系統中所有的變數
1)使用者自定義變數
[root@VM_0_16_centos es]# num2=123
[root@VM_0_16_centos es]# num3=234
2)變數呼叫,使用$xxxName 呼叫變數的值
3)將一個變數賦值給另一個變數
4)將運算的結果賦值給變數
[root@VM_0_16_centos es]# num4=`ll`
[root@VM_0_16_centos es]# echo $num4
total 28 -rwxr-xr-x 1 root root 18 May 10 14:44 firstshell -rw-r--r-- 1 es root 136 May 9 09:28 hello2.txt.tar.gz -rwxrwxrwx 1 es es 156 May 9 12:38 hello.txt -rw-rw-r-- 1 es es 136May 8 19:21 hello.txt.tar.gz -rw-rw-r-- 1 es es 182 May 8 19:12 hello.txt.zip drwxr-xr-x 2es es 4096 May 9 09:32 temp drwxr-xr-x 2 root root 4096 May 8 19:10 tody
使用$()
[root@VM_0_16_centos home]# num5=$(ll /home/es)
[root@VM_0_16_centos home]# echo $num5
total 28 -rwxr-xr-x 1 root root 18 May 10 14:44 firstshell -rw-r--r-- 1 es root 136 May 9 09:28 hello2.txt.tar.gz -rwxrwxrwx 1 es es 156 May 9 12:38 hello.txt -rw-rw-r-- 1 es es 136May 8 19:21 hello.txt.tar.gz -rw-rw-r-- 1 es es 182 May 8 19:12 hello.txt.zip drwxr-xr-x 2es es 4096 May 9 09:32 temp drwxr-xr-x 2 root root 4096 May 8 19:10 tody
[root@VM_0_16_centos home]#
[root@VM_0_16_centos home]# num6=$((4+5))
[root@VM_0_16_centos home]# echo $num6
9
[root@VM_0_16_centos home]#
拼接
[root@VM_0_16_centos home]# str1=123.123.123
[root@VM_0_16_centos home]# str2=root@$str1
[root@VM_0_16_centos home]# echo $str2
root@123.123.123
[root@VM_0_16_centos home]# str3=root@${str1}
[root@VM_0_16_centos home]# echo $str3
root@123.123.123
[root@VM_0_16_centos home]# str4=root@"$str1"
[root@VM_0_16_centos home]# echo str4
str4[root@VM_0_16_centos home]# echo $str4root@123.123.123[root@VM_0_16_centos home]#
轉義使用''單引號
[root@VM_0_16_centos home]# str5=root@'$str1'
[root@VM_0_16_centos home]# echo $str5
root@$str1
[root@VM_0_16_centos home]#
列出所有的變數
set命令
刪除變數
unset
但是,有一種變數無法使用unset刪除
readonly變數:
[root@VM_0_16_centos home]# readonly rnum=123
[root@VM_0_16_centos home]# unset rnum
-bash: unset: rnum: cannot unset: readonly variable
[root@VM_0_16_centos home]#
使用者自定義的變數,作用域只在當前的shell中,如果重啟那麼就失效。
環境變數
作用域:當前shell及子shell
一個shell指令碼包含的內容
1)首行 #!/bin/bash
表示使用bash直譯器
注意:第一行的#特殊,表示定義,其他行則表示註釋
2)註釋
除第一行外的其他行,行首加上#即可
3)內容
建立一個shell指令碼
1)建立一個shell檔案
方法很多:
touch
vi/vim
echo "" > 檔名
2)編輯內容:
#!/bin/bash
#this is my first sehll script
echo "hello shell"
echo "hello scala"
3)執行:會發現沒有執行檔案的許可權
[root@VM_0_16_centos es]# ll
total 28
-rw-r--r-- 1 root root 81 May 10 14:14 firstshell
-rw-r--r-- 1 es root 136 May 9 09:28 hello2.txt.tar.gz
-rwxrwxrwx 1 es es 156 May 9 12:38 hello.txt
-rw-rw-r-- 1 es es 136 May 8 19:21 hello.txt.tar.gz
-rw-rw-r-- 1 es es 182 May 8 19:12 hello.txt.zip
drwxr-xr-x 2 es es 4096 May 9 09:32 temp
drwxr-xr-x 2 root root 4096 May 8 19:10 tody
[root@VM_0_16_centos es]# ./firstshell
bash: ./firstshell: Permission denied
[root@VM_0_16_centos es]# chmod +x firstshell
[root@VM_0_16_centos es]# ./firstshell
hello shell
hello scala
2.三種方式執行指令碼:
1)使用相對路徑或者絕對路徑執行
./firstshell
2)bash firstshell
3)source firstshell
4). firstshell
[root@VM_0_16_centos es]# . firstshell
hello shell
hello scala
[root@VM_0_16_centos es]# bash firstshell
hello shell
hello scala
[root@VM_0_16_centos es]# source firstshell
hello shell
hello scala
這麼多方式,有什麼區別呢?
使用. firstshell會生成一個新的bash環境進行執行
使用./ bash . source這幾種方法,會在當前的環境中執行。
例子:
[root@VM_0_16_centos es]# aa=123
[root@VM_0_16_centos es]# echo $aa
123
[root@VM_0_16_centos es]# echo "echo \$aa" > firstshell
[root@VM_0_16_centos es]# cat firstshell
echo $aa
[root@VM_0_16_centos es]# echo "echo \$aa" >> firstshell
[root@VM_0_16_centos es]# cat firstshell
echo $aa
echo $aa
[root@VM_0_16_centos es]# ./firstshell
[root@VM_0_16_centos es]# bash firstshell
[root@VM_0_16_centos es]# source firstshell
123
123
shell變數:
變數:是shell傳遞資料的一種方式,用來代表每個取值的符號名
shell變數設定規則
不能以數字開頭
變數預設都是字串型別
[root@VM_0_16_centos es]# num3=$num+$num2
[root@VM_0_16_centos es]# echo $num3
12+13
如果變數有空格,需要使用單雙引號
[root@VM_0_16_centos es]# pro=pro fillll
-bash: fillll: command not found
[root@VM_0_16_centos es]# pro="pro fillll"
[root@VM_0_16_centos es]#
=號兩邊不能有空格
[root@VM_0_16_centos es]# lxx = 1
-bash: lxx: command not found
[root@VM_0_16_centos es]# lxx =1
-bash: lxx: command not found
3.分類
使用者自定義變數
環境變數 $HOME,$SHELL,$PWD,$USER等
位置引數變數
預定義變數
使用set檢視系統中所有的變數
1)使用者自定義變數
[root@VM_0_16_centos es]# num2=123
[root@VM_0_16_centos es]# num3=234
2)變數呼叫,使用$xxxName 呼叫變數的值
3)將一個變數賦值給另一個變數
4)將運算的結果賦值給變數
[root@VM_0_16_centos es]# num4=`ll`
[root@VM_0_16_centos es]# echo $num4
total 28 -rwxr-xr-x 1 root root 18 May 10 14:44 firstshell -rw-r--r-- 1 es root 136 May 9 09:28 hello2.txt.tar.gz -rwxrwxrwx 1 es es 156 May 9 12:38 hello.txt -rw-rw-r-- 1 es es 136May 8 19:21 hello.txt.tar.gz -rw-rw-r-- 1 es es 182 May 8 19:12 hello.txt.zip drwxr-xr-x 2es es 4096 May 9 09:32 temp drwxr-xr-x 2 root root 4096 May 8 19:10 tody
使用$()
[root@VM_0_16_centos home]# num5=$(ll /home/es)
[root@VM_0_16_centos home]# echo $num5
total 28 -rwxr-xr-x 1 root root 18 May 10 14:44 firstshell -rw-r--r-- 1 es root 136 May 9 09:28 hello2.txt.tar.gz -rwxrwxrwx 1 es es 156 May 9 12:38 hello.txt -rw-rw-r-- 1 es es 136May 8 19:21 hello.txt.tar.gz -rw-rw-r-- 1 es es 182 May 8 19:12 hello.txt.zip drwxr-xr-x 2es es 4096 May 9 09:32 temp drwxr-xr-x 2 root root 4096 May 8 19:10 tody
[root@VM_0_16_centos home]#
[root@VM_0_16_centos home]# num6=$((4+5))
[root@VM_0_16_centos home]# echo $num6
9
[root@VM_0_16_centos home]#
拼接
[root@VM_0_16_centos home]# str1=123.123.123
[root@VM_0_16_centos home]# str2=root@$str1
[root@VM_0_16_centos home]# echo $str2
root@123.123.123
[root@VM_0_16_centos home]# str3=root@${str1}
[root@VM_0_16_centos home]# echo $str3
root@123.123.123
[root@VM_0_16_centos home]# str4=root@"$str1"
[root@VM_0_16_centos home]# echo str4
str4[root@VM_0_16_centos home]# echo $str4root@123.123.123[root@VM_0_16_centos home]#
轉義使用''單引號
[root@VM_0_16_centos home]# str5=root@'$str1'
[root@VM_0_16_centos home]# echo $str5
root@$str1
[root@VM_0_16_centos home]#
列出所有的變數
set命令
刪除變數
unset
但是,有一種變數無法使用unset刪除
readonly變數:
[root@VM_0_16_centos home]# readonly rnum=123
[root@VM_0_16_centos home]# unset rnum
-bash: unset: rnum: cannot unset: readonly variable
[root@VM_0_16_centos home]#
使用者自定義的變數,作用域只在當前的shell中,如果重啟那麼就失效。
環境變數
作用域:當前shell及子shell
相關文章
- Linux Shell程式設計(3)——執行shell指令碼Linux程式設計指令碼
- shell簡介入門bash程式設計之執行(轉)程式設計
- Linux Bash Shell學習(七):shell程式設計基礎——執行Shell指令碼、functionLinux程式設計指令碼Function
- C#多執行緒程式設計實戰1.1建立執行緒C#執行緒程式設計
- Posix執行緒程式設計指南(1)-執行緒建立與取消 (轉)執行緒程式設計
- python多執行緒程式設計2—執行緒的建立、啟動、掛起和退出Python執行緒程式設計
- .NET多執行緒程式設計(4):執行緒池和非同步程式設計 (轉)執行緒程式設計非同步
- Shell程式設計-11-子Shell和Shell巢狀程式設計巢狀
- Shell程式設計程式設計
- Linux Shell程式設計(1)——shell程式設計簡介Linux程式設計
- Shell程式設計 --- Shell介紹程式設計
- 多執行緒程式設計執行緒程式設計
- 執行緒程式設計(一)執行緒程式設計
- .NET多執行緒程式設計(1):多工和多執行緒 (轉)執行緒程式設計
- iOS多執行緒程式設計三:Operation和OperationQueueiOS執行緒程式設計
- Windows Shell 程式設計Windows程式設計
- shell程式設計(轉)程式設計
- Shell程式設計-shell變數1程式設計變數
- Bourne Shell及shell程式設計(轉)程式設計
- JavaScript多執行緒程式設計JavaScript執行緒程式設計
- Boost多執行緒程式設計執行緒程式設計
- 程式設計師如何執行jivejodn?程式設計師
- UNIX多執行緒程式設計執行緒程式設計
- 多執行緒程式設計(轉)執行緒程式設計
- UNIX環境高階程式設計——執行緒和fork程式設計執行緒
- Shell程式設計-02-Shell變數程式設計變數
- Linux Shell程式設計(27)——子shellLinux程式設計
- Linux 程式設計之Shell程式設計(轉)Linux程式設計
- 併發程式設計之多執行緒執行緒安全程式設計執行緒
- 初識shell程式設計程式設計
- Shell 程式設計入門程式設計
- shell程式設計進階程式設計
- shell程式設計入門程式設計
- shell程式設計基礎程式設計
- Shell程式設計-基礎程式設計
- shell併發程式設計程式設計
- linux shell 程式設計Linux程式設計
- linux shell程式設計Linux程式設計