歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> Unix基礎知識 >> AIX系統SHELL編程基礎

AIX系統SHELL編程基礎

日期:2017/3/3 15:20:45   编辑:Unix基礎知識

什麼是Shell?

Shell 是介於操作系統和用戶之間的界面,它能解釋用戶發出的命令,啟動應用程序,並利用系統功能管理用戶數據。Shell 可以在用戶的交互式控制下在前台或後台同時運行多個進程,Shell 同時也是一種有效的編程語言。

1、通配符

(1)*:多個任意字符

$ ls

info newdir test.file testfile

mbox newtest.file test1.file wangyi

$ ls *.*

newtest.file test.file test1.file

(2)?:單個任意字符

$ ls ????.*

test.file

(3) [ ]:在[ ]之間的單個字符

$ ls [mt]*

mbox test.file test1.file

(4)[ - ]:在[ - ]范圍內的單個字符

$ ls [m-w]*

mbox newtest.file test.file test1.file testfile

newdir:

wangyi:

(5)!:非某個或某些字符

$ ls [!t]*

mbox newtest.file

info:

notes tmp

newdir:

wangyi:

test.file

2、標准輸入輸出

在 AIX 標准狀態下,輸入來自鍵盤,輸出是到屏幕,出錯信息也是顯示在屏幕上。

重定向符號 系統變量名

標准輸入 < (<<) 0

標准輸出 > (>>) 1

標准錯誤輸出 2> (2>>) 2

3/重定向

在 AIX 的環境中標准的輸入、輸出和錯誤信息均可以重新定向,使用戶可以從鍵盤以外的其他地方(如文件,串口等)獲取輸入信息,並把輸入的內容和出錯的信息送到屏幕之外的其他地方(如文件等)。

●輸入重定向

$ command < filename

$ mail xumin < test.file

查看本欄目更多精彩內容:http://www.bianceng.cn/OS/unix/

● 輸出重定向

$ command > filename

$ ls > out.file

$ cat out.file

28

info

mbox

newdir

newtest.file

out.file

test.file

test1.file

testfile

wangyi

● 錯誤信息重定向

$ command 2> filename

$ ls test2.file 2> error.out

$ cat error.out

ls: 0653-341 The file test2.file does not exist.

● 組合重定向

$ command < infile > outfile 2> errfile

$ command > outfile 2> errfile < infile

$ ls test1.file test2.file > out.file 2> error.file

$ cat out.file

test1.file

$ cat error.file

ls: 0653-341 The file test2.file does not exist.

●關聯組合重定向

$ command > outfile 2> &1

這裡的&1 指向 out.file,因為此時原來系統的標准輸出已被重定向為 out.file。

$ command 2> &1 > outfile

這裡的&1 指向標准輸出,此時原來系統的標准輸出尚未改變。

4、管道

管道的作用是把前一個命令的輸出作為後一個命令的輸入。

管道的用法:

$ command1 | command2

$ ls | wc -w

5、分割輸出

tee 這個命令可以讀取標准輸入,並把數據同時送到標准的輸出和指定的文件中

tee 的用法:

$ command1 | tee filename | command2

$ ls | tee out.file | wc

11 11 97

$ cat out.file

error.file

error.out

info

mbox

newdir

newtest.file

out.file

test.file

test1.file

testfile

6、多個命令

在 AIX 中 shell 允許在同一行中寫多個命令,只需要在命令之間加上“;”作為分隔符

用法:

$ command1 ; command2 ; command3

$ pwd;cd /;ls

/home/xumin

Slider.class dead.letterlost+found smit.script usr

TT_DB dev lpp testnfs var

aaaa etc mnt testxmg websm.log

adsmtest home opt testxmg2 xumin

bin info sbin tftpboot xumin_disk1

cdrom info.www share tmp

cds.types informix showcase u

core lib smit.log unix

7、長命令

在 AIX 的 shell 中寫較長的命令時,可以使用“\”作為換行的標記,這時 shell 會用一個“>”作為提示符。

8、Shell 環境中的變量

在 shell 環境下面可以定義變量,供系統、用戶,shell 程序和各種應用使用。變量可以由系統定義,也可以由用戶自己定義。系統已經定義的變量用戶也可以修改或刪除。

例如: HOME -- 用戶的 home 目錄

TERM -- 終端類型

PATH -- 命令搜索路徑

9、變量的查看和定義

(1)查看已定義過的變量

$ set

~~~

~~~

HOME=/home/xumin

LANG=En_US

LOCPATH=/usr/lib/nls/loc

LOGIN=xumin

LOGNAME=xumin

MAIL=/usr/spool/mail/xumin

MAILCHECK=600

MAILMSG='[YOU HAVE NEW MAIL]'

~~~

~~~

PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/home/xumin/bin:/usr/bin/X11:/sbin:.

PPID=69504nput

PS1='$ '

PS2='> '

PS3='# '

PS4='+ '

PWD=/

RANDOM=31884

32

SECONDS=2774

SHELL=/usr/bin/ksh

TERM=ANSI

TERM_DEFAULT=lft

TMOUT=0

TZ=CST6CDT

USER=xumin

(2)顯示變量值

$ echo $name

$ echo $HOME

/home/xumin

(3)定義變量

$ name=value

$ echo $xxx

$ xxx=hello!

$ echo $xxx

hello!

(4)刪除變量

$ unset

$ unset xxx

$ echo $xxx

(5)變量的運用

' ':把' '之間的內容作為一個命令,返回命令的結果

$ now=`date`

$ echo $now

Wed Aug 12 15:23:19 CDT 1998

' ' :不解釋 ' ' 之間的任何內容

$ echo '$HOME'

$HOME

“”:會解釋“”之間的 $、` `、\等字符的特殊含義

$ echo “now is `date`”

now is Wed Aug 12 15:32:41 CDT 1998

\:忽略\之後的特殊字符的特殊含義

$ echo \$HOME

$HOME

Copyright © Linux教程網 All Rights Reserved