轉帖|行業資訊|編輯:龔雪|2015-10-16 11:47:18.000|閱讀 268 次
概述:shell腳本可以直接與操作系統內核打交道,從而完成任意復雜的任務,讓我們一起來看看shell是如何完成某些特殊復雜的功能的。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
同C、C++等語言一樣,shell腳本也提供了數組這樣一個重要的數據結構,shell中的數組有兩種,一種為普通數組,另外的一種稱為關聯數組。普通數據的存取通過整數進行,關聯數組的存取通過字符串進行。具體如下:
//用()定義一個數組,注意數組元素間不能用,否則達不到預期目的 root@sparkmaster:~/ShellLearning/chapter11# arr=(1 2 3 4 5 6) root@sparkmaster:~/ShellLearning/chapter11# echo ${arr[0]} 1 //用,號的話,數組只有一個元素 root@sparkmaster:~/ShellLearning/chapter11# arr=(1,2,3,4,5,6) root@sparkmaster:~/ShellLearning/chapter11# echo ${arr[0]} 1,2,3,4,5,6
除了()定義數組外,還可以采用逐個賦值的方法,例如
root@sparkmaster:~/ShellLearning/chapter11# strArr[0]="hello" root@sparkmaster:~/ShellLearning/chapter11# strArr[1]="hello1" root@sparkmaster:~/ShellLearning/chapter11# echo ${strArr[0]} hello
上面演示了如何輸出單個數組內容,有時可能想輸出數組中的所有內容及數組的長度,代碼如下:
//用*號將輸出數組中的所有內容 root@sparkmaster:~/ShellLearning/chapter11# echo ${strArr[*]} hello hello1 //${#strArr[*]}取得數組的長度 root@sparkmaster:~/ShellLearning/chapter11# echo ${#strArr[*]} 2
關聯數組的定義與普通數組不一樣,關聯數組需要使用declare命令進行聲明,具體如下:
//declare -A associative_array聲明一個關聯數組 root@sparkmaster:~/ShellLearning/chapter11# declare -A associative_array //填充內容 root@sparkmaster:~/ShellLearning/chapter11# associative_array=([key1]=value1 [key2]=value2 [key3]=value3) //獲取關聯數組內容 root@sparkmaster:~/ShellLearning/chapter11# echo ${associative_array[key1]} value1
在實際使用時,常常需要確定數組的索引值,具體使用代碼如下:
//獲取關聯數組的索引 root@sparkmaster:~/ShellLearning/chapter11# echo ${!associative_array[*]} key3 key2 key1 //獲取普通數組的索引 root@sparkmaster:~/ShellLearning/chapter11# echo ${!strArr[*]} 0 1
在使用shell命令時,有時候命令太長,要記住它非常困難,此時可以采用shell命令別名的方式。事實上,linux系統自身已經自帶了很多命令別名,例如~/.bashrc已經幫我們設置了多個命令別名,vim打開該文件,可以看到下面的代碼
# enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' #alias dir='dir --color=auto' #alias vdir='vdir --color=auto' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' fi # some more ls aliases alias ll='ls -alF' alias la='ls -A' alias l='ls -CF'
代碼中的命令alias表示的是給命令取別名,例如alias ll=’ls -alF’,表示ll是命令’ls -alF’的別名
//ll命令等同于ls -alF命令 root@sparkmaster:~/ShellLearning/chapter11# ll total 8 drwxr-xr-x 2 root root 4096 2015-10-04 23:21 ./ drwxr-xr-x 5 root root 4096 2015-10-04 23:21 ../ root@sparkmaster:~/ShellLearning/chapter11# ls -alF total 8 drwxr-xr-x 2 root root 4096 2015-10-04 23:21 ./ drwxr-xr-x 5 root root 4096 2015-10-04 23:21 ../
ubuntu linux 安裝某個軟件包的命令是sudo apt-get install,如果覺得它比較難記,可以直接使用alias給它命名一個好記的別名,例如
//給sudo apt-get install取個別名install root@sparkmaster:~/ShellLearning/chapter11# alias install='sudo apt-get install' //直接使用install命令代替sudo apt-get install命令 root@sparkmaster:~/ShellLearning/chapter11# install opencv //但是需要注意的是在終端取別名,一旦終端關閉,別名命令不會保存 //如果想永久使用即開機后該別名命令就生效的話,則需要將別名命令重定向 //保存到~/.bashrc文件中 root@sparkmaster:~/ShellLearning/chapter11# echo 'alias install="sudo apt-get install"' >> ~/.bashrc
shell命令有許多時間操作命令,具體使用如下
//查看當前時間 root@sparkmaster:~/ShellLearning/chapter11# date Mon Oct 5 00:06:11 PDT 2015 //查看當前是星期幾 root@sparkmaster:~/ShellLearning/chapter11# date +%A Monday //查看當前月份 root@sparkmaster:~/ShellLearning/chapter11# date +%B October //查看當前是當月的第幾天 root@sparkmaster:~/ShellLearning/chapter11# date +%d 05
date命令參數列表如下:
參數
|
參數作用
|
%a 簡寫法,例如Monday,簡寫為Mon;%A,全拼法Monday
|
Weekday |
%b 簡寫法,例如October,簡寫為Otc;%B,全拼法October
|
Month |
%d
|
Day |
%D
|
格式化日期 |
%y,簡寫法,例如2010,簡寫為10;%Y,全寫法2010
|
Year |
%I 或%H
|
Hour |
%M
|
Minute |
%S
|
Second |
%N
|
Nano second |
%s
|
Unix系統時間 |
//按年月日將時間格式化輸出 root@sparkmaster:~/ShellLearning/chapter11# date "+%Y %B %d" 2015 October 05
shell命令中還有一個命令很重要,那就是sleep命令,常用它來進行延遲腳本的執行,下面的例子來自linux shell scripting cookbook
#!/bin/bash #Filename: sleep.sh echo -n count: #保存終端光標位置 tput sc count=0; while true; do #count小于40,則繼續執行 if [ $count -lt 40 ]; then let count++; #延遲一秒再執行 sleep 1; #恢復光標位置 tput rc #清除光標位置內容 tput ed #將count值顯示到光標位置 echo -n $count; else exit 0; fi done
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn