(6-1)shell编程 (1)


1、shell编程的hello word!
[root@baolibin shell]# pwd
/usr/local/shell

[root@baolibin shell]# vim hello.sh

[root@baolibin shell]# chmod +x hello.sh


[root@baolibin shell]# more hello.sh
#!/bin/bash
#hello word
echo 'hello word!'


[root@baolibin shell]# ./hello.sh
hello word!


2、变量分类:
本地变量
环境变量
局部变量
位置变量
特殊变量


[root@baolibin shell]# echo $JAVA_HOME
/usr/local/jdk
[root@baolibin shell]# echo $HADOOP_HOME
/usr/hadoop
[root@baolibin shell]# echo ${JAVA_HOME}
/usr/local/jdk
[root@baolibin shell]# echo $JAVA_HOMEi love computer
love computer
[root@baolibin shell]# echo ${JAVA_HOME}i love computer
/usr/local/jdki love computer
[root@baolibin shell]#



2.1、本地变量:只对当前shell进程有效,对子进程和其它shell进程无效。
定义:VAR_NAME=VALUE
变量引用:${VAR_NAME}
取消变量:unset VAR_NAME

[root@baolibin shell]# baozi=www.hadoop.com
[root@baolibin shell]# echo $baozi
www.hadoop.com
[root@baolibin shell]# pstree
init─┬─NetworkManager
     ├─abrtd
     ├─acpid
     ├─atd
     ├─auditd───{auditd}
     ├─automount───4*[{automount}]
     ├─bluetoothd
     ├─bonobo-activati───{bonobo-activat}
     ├─certmonger
     ├─console-kit-dae───63*[{console-kit-da}]
     ├─crond
     ├─cupsd
     ├─2*[dbus-daemon───{dbus-daemon}]
     ├─dbus-launch
     ├─devkit-power-da
     ├─gconfd-2
     ├─gdm-binary─┬─gdm-simple-slav─┬─Xorg
     │            │                 ├─gdm-session-wor
     │            │                 ├─gnome-session─┬─at-spi-registry
     │            │                 │               ├─gdm-simple-gree
     │            │                 │               ├─gnome-power-man
     │            │                 │               ├─metacity
     │            │                 │               ├─plymouth-log-vi
     │            │                 │               ├─polkit-gnome-au
     │            │                 │               └─{gnome-session}
     │            │                 └─{gdm-simple-sla}
     │            └─{gdm-binary}
     ├─gnome-settings-───{gnome-settings}
     ├─gvfsd
     ├─hald─┬─hald-runner─┬─hald-addon-acpi
     │      │             ├─hald-addon-inpu
     │      │             └─hald-addon-rfki
     │      └─{hald}
     ├─5*[mingetty]
     ├─modem-manager
     ├─mysqld_safe───mysqld───15*[{mysqld}]
     ├─polkitd
     ├─pulseaudio───2*[{pulseaudio}]
     ├─rpc.statd
     ├─rpcbind
     ├─rsyslogd───3*[{rsyslogd}]
     ├─rtkit-daemon───2*[{rtkit-daemon}]
     ├─sshd───sshd───bash───pstree
     ├─udevd───2*[udevd]
     └─wpa_supplicant
[root@baolibin shell]# bash
[root@baolibin shell]# echo $baozi

[root@baolibin shell]# exit
exit
[root@baolibin shell]# echo $baozi
www.hadoop.com
[root@baolibin shell]# unset baozi
[root@baolibin shell]# echo $baozi

[root@baolibin shell]#




2.2、环境变量:自定义的环境变量对当前shell进程及其子shell进程有效,对其它的shell进程无效。
定义:export VAR_NAME=VALUE
对所有shell进程都有效需要配置到配置文件中:vi /etc/profile      source /etc/profile


[root@baolibin shell]# export xiaobaozi=www.hbase.com
[root@baolibin shell]# echo $xiaobaozi
www.hbase.com
[root@baolibin shell]# bash
[root@baolibin shell]# echo $xiaobaozi
www.hbase.com
[root@baolibin shell]# exit
exit
[root@baolibin shell]# echo $xiaobaozi
www.hbase.com
[root@baolibin shell]# unset xiaobaozi
[root@baolibin shell]# echo $xiaobaozi

[root@baolibin shell]#



2.3、局部变量:
函数调用结束,变量就会消失。
对shell脚本中某代码片段有效。
定义:local VAR_NAME=VALUE。







2.4、位置变量:
$1,$2......${10}...
/test.sh a b
$0:脚本本身
$1:脚本的第一个参数
$2:脚本的第二个参数




[root@baolibin shell]# vi test.sh
[root@baolibin shell]# more test.sh
#!/bin/bash
#--------------
echo $0
echo $1
echo $2
[root@baolibin shell]# chmod +x test.sh
[root@baolibin shell]# ./test.sh 4 5
./test.sh
4
5
[root@baolibin shell]# ./test.sh 4 5 6
./test.sh
4
5
[root@baolibin shell]#





2.5、特殊变量:
$?:接收上一条命令的返回状态码
$#:参数个数
$*:或者$@:所有的参数



$?:接收上一条命令的返回状态码:

[root@baolibin shell]# ls
hello.sh  test.sh
[root@baolibin shell]# echo $?
0
[root@baolibin shell]# hehehaha
-bash: hehehaha: command not found
[root@baolibin shell]# echo $?
127
[root@baolibin shell]# echo $?
0
[root@baolibin shell]#



$#:参数个数:

[root@baolibin shell]# vi count.sh
[root@baolibin shell]# more count.sh
#!/bin/bash
#----------
echo $#
[root@baolibin shell]# chmod +x count.sh
[root@baolibin shell]# ./count.sh
0
[root@baolibin shell]# ./count.sh a
1
[root@baolibin shell]# ./count.sh a b
2
[root@baolibin shell]# ./count.sh a b c
3
[root@baolibin shell]#



$*:或者$@:所有的参数:

[root@baolibin shell]# vi alloy.sh
[root@baolibin shell]# more alloy.sh
#!/bin/bash
#---------
echo $#
echo $*
echo $@
[root@baolibin shell]# chmod +x alloy.sh
[root@baolibin shell]# ./alloy.sh
0


[root@baolibin shell]# ./alloy.sh a
1
a
a
[root@baolibin shell]# ./alloy.sh a b
2
a b
a b
[root@baolibin shell]#






2.6:单引号、双引号、反引号
''单引号不解析变量。
""双引号会解析变量。
``反引号是执行并引用一个命令的执行结果,类似于$(...)。


[root@baolibin shell]# echo $baozi

[root@baolibin shell]# baozi=www.hive.com
[root@baolibin shell]# echo $baozi
www.hive.com
[root@baolibin shell]# echo '$baozi'
$baozi
[root@baolibin shell]# echo "$baozi"
www.hive.com
[root@baolibin shell]# echo `$baozi`
-bash: www.hive.com: command not found

[root@baolibin shell]# 





[root@baolibin shell]# echo `ls`
alloy.sh count.sh hello.sh test.sh
[root@baolibin shell]# ls
alloy.sh  count.sh  hello.sh  test.sh
[root@baolibin shell]# echo $(ls)
alloy.sh count.sh hello.sh test.sh
[root@baolibin shell]#



[root@baolibin shell]# echo $(ll)
总用量 16 -rwxr-xr-x. 1 root root 47 4月 18 22:07 alloy.sh -rwxr-xr-x. 1 root root 32 4月 18 22:04 count.sh -rwxr-xr-x. 1 root root 43 4月 16 22:51 hello.sh -rwxr-xr-x. 1 root root 52 4月 18 21:58 test.sh
[root@baolibin shell]# ll
总用量 16
-rwxr-xr-x. 1 root root 47 4月  18 22:07 alloy.sh
-rwxr-xr-x. 1 root root 32 4月  18 22:04 count.sh
-rwxr-xr-x. 1 root root 43 4月  16 22:51 hello.sh
-rwxr-xr-x. 1 root root 52 4月  18 21:58 test.sh
[root@baolibin shell]#









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值