歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Shell環境學習筆記

Shell環境學習筆記

日期:2017/3/1 9:17:48   编辑:SHELL編程

在Unix式的操作系統中有一個最重要的特性就是命令行界面或shell。shell環境使得用戶能與操作系統的核心功能進行交互。術語腳本更多涉及的便是這種環境。編寫腳本通常使用某種基於解釋器的編程語言。而shell腳本不過就是一些文件,我們能將一系列需要執行的命令寫入其中,然後通過shell來執行。目前大多數GUN/Linux系統默認的shell環境是bash。在linux系統中,命令都是在shell終端中輸入並執行的。打開終端後就會出現提示符:

[root@localhost ~]#

1.其中的root是用戶名;

2.#代表管理員用戶root,$代表普通用戶;

shell腳本通常是一個以shebang起始的文本文件,如:

#!/bin/bash 其中#!位於解釋器路徑之前。/bin/bash是Bash的解釋器命令路徑。

有兩種運行腳本的命令:

(1)將腳本作為bash的命令行參數,即:bash 文件名

(2)賦予腳本可執行的權限,然後再執行:chmod +x 文件名 文件路徑/文件名

終端打印命名:echo和printf

echo是用於終端打印的基本命令,並且默認執行後會換行。

echo "hello world"  echo 'hello world'  echo hello world這三種形式都可以成功地輸出結果,但是各有細微的差別

[root@localhost ~]# echo hello world
hello world
[root@localhost ~]# echo "hello world"
hello world
[root@localhost ~]# echo 'hello world'
hello world

如果想要打印 ! ,就不能把 ! 放在雙引號裡,可以在前面加轉義字符\或者直接輸出

[root@localhost ~]# echo !
!
[root@localhost ~]# echo \!
!
[root@localhost ~]# echo "!"
bash: !: event not found
[root@localhost ~]# echo '\!'
\!

如果在輸出的語句中帶有;,就不能直接輸出

[root@localhost ~]# echo "hello;world"
hello;world
[root@localhost ~]# echo 'hello;world'
hello;world
[root@localhost ~]# echo hello;world
hello
bash: world: command not found

printf語句默認執行後不換行,在不帶引號輸出的時候,不能直接輸出帶有空格的語句

[root@localhost ~]# printf "hello world"
hello world[root@localhost ~]# printf hello world
hello[root@localhost ~]# printf helloworld
helloworld[root@localhost ~]# printf 'hello world'
hello world[root@localhost ~]# printf hello world
hello

printf還可以定義輸出格式

[root@localhost ~]# printf '%-5s %-10s\n' 123 456
123 456
[root@localhost ~]# printf "%-5s %-10s\n" 123 456
123 456

Copyright © Linux教程網 All Rights Reserved