Shell 的歷史:bsh -àBourn Again Shell
csh
ksh
bsh的特性:
命令歷史,別名,管道,重定向
Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋體;mso-fareast-theme-font: minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">交互是的執行命令;
Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋體;mso-fareast-theme-font: minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">編程環境 自動化的應用
流程式控制制:順序執行;選擇執行;循環執行.
靜態語言,編譯型語言;
編譯,鏈接,運行效率高
動態語言,解釋型語言(解釋器ksh,bsh,csh);腳本語言,解釋器不需要編譯,可以直接運行;運行效率低,便於維護和修改.
所以我們在進行腳本編寫的時候,在第一行,必須指定使用哪一種解釋器
在Linux上,開發大型的應用,需要藉助Perl,Python.
Perl:
Python:完全面向對象,語法、段落縮進、代碼結構清晰,易於維護,並且可以開發Web應用.
#!/bin/bash
變數:variable 變數是什麼? --命名內存空間
如何引用變數的值? $A
對於計算機來講,數值和字元有嚴格區別.
變數類型直接決定佔用多大內存空間,決定了存儲的數據類型,在腳本中當作字元來存儲.
bash:
本地變數(局部變數) 只對當前shell有效 可以使用set 來查看當前的本地變數,取消環境變數為unset
環境變數 對
export= VARIABLE_NAME=Value 指定環境變數
位置變數
特殊變數($?)
怎麼命名變數:變數的名稱只能包含字母,數字,下劃線,卻只能以字母下劃線進行命名.
注意:1.儘可能避開系統已用變數名
2.編程風格要相同
變數替換:echo $NAME
“ ”,弱引用,變數替換會進行
‘’強引用,變數替換會禁止
` ` 表示命令替換,把一個命令的執行結果直接傳遞給另外一個命令.
顯示所有環境變數
#export 用於顯示所有環境變數
#printenv 用於顯示系統所有的環境變數
#set 用於查看環境變數與自定義變數
SHELL,process,prompt 進程都是有父進程fork自身而來.我們打開的每一個命令都屬於shell
的子進程.
本地變數:
環境變數:
變數類型:整數,字元,浮點,布爾型
SHELL:(內部命令,外部命令)
系統定義的環境變數:為了shell的自身運行,而配置的參數.
#declare –x VARIABLE 聲明一個環境變數
-I integer 定義個整型
-a array 定義數組
#unset VARIABLE_NAME 在使用完變數之後,應該顯示撤銷.變數的生命周期是shell.
全局配置文件:
/etc/profile, /etc/profile.d/*, /etc/bashrc (全局配置文件,profile.d/*肢解成片段,全局配置文件)
個人配置文件:
~/.bash_profile, ~/.bashrc, ~/.bash_logout->用戶退出時的配置文件,前兩個是用戶登錄所啟動的配置文件.
SHELL啟動的類型:
互動式登錄shell 用戶通過控制台 su – 互動式
非互動式登錄
互動式登錄:
/etc/profile --> /etc/profile.d/* --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc
非互動式登錄:
~/.bashrc --> /etc/bashrc --> /etc/profile.d/*
profile 類文件中定義什麼:
mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family: Calibri;mso-hansi-theme-font:minor-latin">設定環境變數
mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family: Calibri;mso-hansi-theme-font:minor-latin">運行一些命令 shell登錄過程中運行的命令
bashrc 類的文件定義:
mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family: Calibri;mso-hansi-theme-font:minor-latin">本地變數
mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family: Calibri;mso-hansi-theme-font:minor-latin">命令別名
[root@station30 ~]# export
declare -x CVS_RSH="ssh"
declare -x DISPLAY="localhost:10.0" 顯示處於何種終端10,0(0表示屏幕號)
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000" 命令歷史
declare -x HOME="/root" 當前用戶的家目錄
declare -x HOSTNAME="station30.example.com" 主機名稱
declare -x INPUTRC="/etc/inputrc" 輸入設備的配置文件
declare -x LANG="en_US.UTF-8" 語言和字符集 #local –a 顯示當前系統上支持的字符集,可以通過編輯
declare -x LESSOPEN="|/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
declare -x LS_COLORS="no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:"
declare -x MAIL="/var/spool/mail/root"
declare -x OLDPWD
declare -x PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" 在shell中鍵入的命令對應的可執行搜索路徑
註:#export PATH=$PATH:/root #export PATH=$PATH:/usr/local/mysql/bin
#export PATH=/root:/$PATH
我們亦可通過編輯環境變數來定義路徑:
vim /etc/profile 編輯之後對於當前shell不會立即生效.讓shell 重讀一下該文件.可以使用
declare -x PWD="/root"
declare -x SHELL="/bin/bash" 當前正在執行的shell
declare -x SHLVL="1"
declare -x SSH_ASKPASS="/usr/libexec/openssh/gnome-ssh-askpass"
declare -x SSH_CLIENT="192.168.0.217 4678 22"
declare -x SSH_CONNECTION="192.168.0.217 4678 172.16.31.1 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x USER="root"
basename 基名,該命令可以去除一個路徑的基名.
[root@station30 ~]# basename /etc/sysconfig/i18n
i18n i18n即為基名
命令別名:
alias name='command'
#alias grep=”grep –color=auto”
unalias name
bash 的登錄與歡迎信息:
可以編輯當前目錄下的./bash_profile 我們可以在該文件中添加一些登錄歡迎語句.
登錄界面的歡迎信息: -----全局變數
#vi /etc/issue
在用戶登錄后所顯示的信息 -----全局變數
#vi /etc/motd
腳本的執行方式:
1.#chmod x
#./script_name
2.#bash ./script
#bash –n SHELL_FILE 分析shell 腳本是否有錯誤
#bash –x SHELL_FILE 執行shell 腳本的過程
命令一條條執行我們稱之為順序執行:
循環:
for 遍歷式循環
for VAR in LIST;do
statement1
statement2
…
done
如何定義列表:
mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family: Calibri;mso-hansi-theme-font:minor-latin">簡單列出
mso-fareast-font-family:宋體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family: Calibri;mso-hansi-theme-font:minor-latin">可以查看一個文件中的所有行,如果有空格被識別為兩行 `ls /var` 命令替換的方式獲取列表
Practice 1:設置10個用戶,並且設置的用戶名是一樣的.
Practice2: 寫一個腳本:
1、設定變數
2、依次向/etc/passwd中的每個用戶問好,並顯示對方的shell,形如: (提示:LINES=`wc -l /etc/passwd | cut -d" " -f1`)
Hello, root, your shell: /bin/bash
3、統計一共有多少個用戶
#!/bin/bash
#
FILE='/etc/passwd'
LINES=`wc -l $FILE | cut -d' ' -f1`
for LINE in `seq 1 $LINES`;do
MYUSER=`head -$LINE $FILE |tail -1 |cut -d: -f1`
MYSHELL=`head -$LINE $FILE |tail -1 |cut -d: -f7`
echo "Hello,$MYUSER,Your shell:$MYSHELL"
done
本文出自 「Andy的IT江湖」 博客,請務必保留此出處http://andyxuchina.blog.51cto.com/4384553/790577
[火星人 ] Shell編程初學已經有532次圍觀