您的位置:首页 > 运维架构 > Shell

Shell基础学习

2015-11-30 17:09 525 查看
Shell基础 学习笔记

#从 HelloWorld 开始
#!/bin/bash
echo "Hello World !"

##1、关于变量
#shell中定义变量的方式为:变量名=变量值(中间没有空格,变量无需修饰词)
#shell中取变量的方式为:$变量名(当变量值中包含空格时需要加引号)
#例如:
skill="iOS Objective-C" #定义了skill变量并赋值
echo $skill #显示skill变量的值,结果为:iOS Objective-C

##2、shell中字符串及其操作
#字符串定义中可以使用单引号和双引号,但是:
#(1)单引号中的字符会原样输出,转义符和变量都是无效的
#(2)双引号中可以有转义符和变量

#字符串拼接
my_name="tom"
your_name="junry"
echo "our_names:"$my_name $your_name

#获取字符串长度
exp_string="congratulation"
echo "length:"${#exp_string}

#截取字符串
echo ${exp_string:1:3}

#3、Shell中的echo命令
#显示不换行
echo -e "OK! \c" # -e表示开启转义 \c表示不换行
echo "it is good"

#显示结果定向至文件
echo "it is good" > file

#原样输出字符串,不进行转义或取变量(使用单引号)
echo -e '$name\"\n'

#显示当前日期
echo `date`

##Shell test命令
#数值测试
num1=100
num2=200
if test ${num1} -eq ${num2}
then
echo "they r equal"
else
echo "they r not equal"
fi
#补充:
#参数         说明
#-eq        等于则为真
#-ne        不等于则为真
#-gt        大于则为真
#-ge        大于等于则为真
#-lt        小于则为真
#-le        小于等于则为真

#字符串测试
str1=100
str2=200
if test str1=str2
then
echo 'they r equal'
else
echo 'they r not equal'
fi
#补充说明:
#参数     说明
# =     等于则为真
# !=        不相等则为真
# -z    字符串 字符串长度伪则为真
# -n    字符串 字符串长度不伪则为真

#文件测试
if test -e ./bash
then
echo 'this file already exists'
else
echo 'this file does not exists'
fi
#补充:
#参数             说明
#-e 文件名     如果文件存在则为真
#-r 文件名     如果文件存在且可读则为真
#-w 文件名     如果文件存在且可写则为真
#-x 文件名     如果文件存在且可执行则为真
#-s 文件名     如果文件存在且至少有一个字符则为真
#-d 文件名     如果文件存在且为目录则为真
#-f 文件名     如果文件存在且为普通文件则为真
#-c 文件名     如果文件存在且为字符型特殊文件则为真
#-b 文件名     如果文件存在且为块特殊文件则为真

##4、Shell流程控制
#Shell中的流程控制不可为空
#else分支如果没有语句执行,就不要写这个else
例如:
if condition
then
command1
command2
command3
...
commandN
fi

#写成一行
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi

#for循环
for skill in Objective-C Java C++ Shell
do
echo "I'm good at $skill"
done
#显示结果:
#I'm good at Objective-C
#I'm good at Java
#I'm good at C++
#I'm good at Shell

##Shell 函数
#注意:shell中的函数在使用前必须是已经定义的,所以要将Shell的函数写在脚本开始部分。
#-------1、函数可以使用function func(),也可以直接func()定义
#-------2、对于参数返回,可以显示的添加return来返回。如果不加return,则将最后一行命令运行结果作为返回值。
#函数的定义格式如下:
[function] funcname [()] {
action;
[return init;]
}

#例如:
#!/bin/bash

#----function without parameter----
firs
b097
tFunc (){
echo "This is the first Shell function"
}

echo "---do firstFunc start---"
firstFunc #调用函数仅使用其函数名即可
echo "---do firstFunc end---"
#----function without parameter----

#----function with return----
funcWithReturn () {
echo "This function will return a sum of two numbers"
echo "please input a number"
read aNum
echo "please input another number"
read anotherNum
echo "the two numbers you inputed are: $aNum and $anotherNum"
return $(($aNum + $anotherNum))
}
echo "----do funcWithReturn start---- "
funcWithReturn
echo "the sum of two numbers you inputed is: $?"#函数返回值在调用该函数后通过$?来获得
echo "----do funcWithReturn end---- \n"
#----function with return----

#----function with parameters----
#In Shell,you can pass the parameter when the function called and you can also get the value of parameter using $n in the function body
#注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数
funcWithParameter () {
echo "the first parameter is: $1"
echo "the second parameter is: $2"
echo "the third parameter is: $3"
echo "..."
echo "the tenth parameter is: ${10}"
echo "the eleventh parameter is: ${11}\n"
}
echo "----do funcWithParameter start----"
funcWithParameter 1 2 3 4 5 6 7 8 9 10 11 #call function with eleven parameters
echo "----do funcWithParameter end----\n"
#----function with parameters----

##Shell 文件包含
#语法格式为:
# . filename --在点和filename之间有空格
#或
# source filename
#例如:test1.sh的代码如下:
#!/bin/bash
url="www.baidu.com"

#test2.sh的代码如下:
#!/bin/bash
. test1.sh #使用点符号来引入test1.sh
echo $url\n


举例:使用df -g检查磁盘空间,并将输出内容写入文件

#----------------------------------------------------------------------------------------------
#the function used for checking disk

DATE=`date "+%Y-%m-%d %H:%M:%S"`
#sed -r 's/([0-9]{4}([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2}))/\1-\2-\3 \4:\5:6:/' $DATE
#DATE=`date "+%Y-%m-%d %H:%M:%S"`
echo "\n$DATE" >> ./logfile.log
df -g >> ./logfile.log
echo "-----------------" >> ./logfile.log
echo "information writed to logfile.log successfully!"
#----------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell