同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"' >> ~/.bashrcshell命令有许多时间操作命令,具体使用如下
//查看当前时间 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 05date命令参数列表如下:
参数作用参数Weekday%a 简写法,例如Monday,简写为Mon;%A,全拼法MondayMonth%b 简写法,例如October,简写为Otc;%B,全拼法OctoberDay%d格式化日期 (mm/dd/yy)%DYear%y,简写法,例如2010,简写为10;%Y,全写法2010Hour%I 或%HMinute%MSecond%SNano second(毫秒)%NUnix系统时间%s //按年月日将时间格式化输出 root@sparkmaster:~/ShellLearning/chapter11# date "+%Y %B %d" 2015 October 05shell命令中还有一个命令很重要,那就是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 相关资源:python入门教程(PDF版)