shell

shell脚本编写规范

多使用内部命令
常用的内部命令有: echo eval exec export read shift exit

echo:可在屏幕上输出信息

echo参数选项 说明
-n 不换行输出内容
-e 解析转义字符
转义字符 说明
\n 换行
\r 回车
\t 制表符
\b 退格
\v 纵向制表符
[root@localhost test]# echo -n i have a cat
i have a cat[root@localhost test]# 
[root@localhost test]# echo i\thave\ta\tcat
ithavetatcat
[root@localhost test]# echo -e i\thave\ta\tcat
ithavetatcat
[root@localhost test]# echo -e "i\thave\ta\tcat"
i	have	a	cat
[root@localhost test]# echo -n {1..6}
1 2 3 4 5 6[root@localhost test]# 

 eval:命令格式:eval args

功能:当 shell 程序执行到 eval 语句时, shell 读入参数 args ,并将它们组合成一个新的命令,然后执
行。
[root@localhost test]# a='shell;head -1 /etc/passwd'
[root@localhost test]# echo $a
shell;head -1 /etc/passwd
[root@localhost test]# eval echo $a
shell
root:x:0:0:root:/root:/bin/bash

exec:exec命令能够在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该进程就终止了。

[root@localhost test]# exec cd /home

Connection closed.

Disconnected from remote host(192.168.10.128:22) at 20:03:30.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

export

[root@localhost test]# export
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/0/bus"
declare -x DISPLAY="localhost:10.0"
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="localhost.localdomain"
declare -x LANG="en_US.UTF-8"
declare -x LESSOPEN="||/usr/bin/lesspipe.sh %s"
declare -x LOGNAME="root"
declare -x LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.m4a=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.oga=01;36:*.opus=01;36:*.spx=01;36:*.xspf=01;36:"
declare -x MAIL="/var/spool/mail/root"
declare -x OLDPWD="/root"
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
declare -x PWD="/root/test"
declare -x SELINUX_LEVEL_REQUESTED=""
declare -x SELINUX_ROLE_REQUESTED=""
declare -x SELINUX_USE_CURRENT_RANGE=""
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_ASKPASS="/usr/libexec/openssh/gnome-ssh-askpass"
declare -x SSH_CLIENT="192.168.10.1 54521 22"
declare -x SSH_CONNECTION="192.168.10.1 54521 192.168.10.128 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TC_LIB_DIR="/usr/lib64/tc"
declare -x TERM="xterm"
declare -x USER="root"
declare -x XDG_DATA_DIRS="/root/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share"
declare -x XDG_RUNTIME_DIR="/run/user/0"
declare -x XDG_SESSION_ID="8"
declare -x which_declare="declare -f"

read:read命令可从标准输入读取字符串等信息,传给shell程序内部定义的变量。

[root@localhost test]# read -p "please input a string: " -t 5 str_data
please input a string: lili
[root@localhost test]# echo $str_data
lili
[root@localhost test]# echo -n "please input your name:"; read name1 name2
please input your name:haha hehe
[root@localhost test]# echo $name1
haha
[root@localhost test]# echo $name2
hehe
[root@localhost test]# 
[root@localhost test]# read name1
xian
[root@localhost test]# echo $name1
xian
shift: 在程序中每使用一次 shift 语句,都会使所有的位置参数依次向左移动一个位置,并使位置参
$# 1 ,直到减到 0 为止。

[root@localhost test]# vim shift_test.sh
[root@localhost test]# bash shift_test.sh 123 456
2

[root@localhost test]# bash 8.17.sh 123 456
2
123 456
8.17.sh
123
1
456

反引号  $(): 显示当前工作路径

[root@localhost test]# data=`pwd`
[root@localhost test]# echo $data
/root/test
[root@localhost test]# data=$(pwd)
[root@localhost test]# echo $data
/root/test

exit:退出shell程序。在exit之后可以有选择地指定一个数作为返回状态

[root@localhost test]# vim exit_test.sh
[root@localhost test]# bash exit_test.sh
222

 代码缩进

[root@localhost test]# vim ~/.vimrc

执行脚本的方法

[root@localhost test]# vim test_1.sh
#!/bin/bash
cd /tmp
pwd
[root@localhost test]# ls -l test_1.sh 
-rw-r--r--. 1 root root 23 Aug 17 11:20 test_1.sh

 1、bash ./filename.sh(产生子进程,再运行,使用当前指定的bash shell去运行)

[root@localhost test]# bash test_1.sh 
/tmp

2、./filename.sh(产生子进程,再运行,使用脚本里面指定的shell去运行。使用该种方式执行需要x权限)

[root@localhost test]# chmod +x test_1.sh 
[root@localhost test]# ./test_1.sh 
/tmp

3、source ./filename.sh(source命令是一个shell内部命令,其功能是读取指定的shell程序文件,并且依次执行其中的所有的语句,并没有创建新的子shell进程,所以脚本里面所有创建的变量都会保存到当前的shell里面)

[root@localhost test]# source test_1.sh 
/tmp

4、. filename.sh(和source一样,也是使用当前进程执行)

[root@localhost test]# . test_1.sh 
/tmp

变量的类型

Shell 是一种动态类型语言和弱类型语言 ,即在 Shell 中,变量的数据类型毋需显示地声明,变量的
数据类型会根据不同的操作有所变化。准确地讲, Shell 中的变量是不分数据类型的,统一地按照字符串
存储。但是根据变量的上下文环境,允许程序执行一些不同的操作,例如字符串的比较和整数的加减等
等。
什么是弱类型语言、强类型语言?
强类型和弱类型主要是站在变量类型处理的角度进行分类的。强类型是指不允许隐式变量类型转换,弱
类型则允许隐式类型转换。
强类型语言,当你定义一个变量是某个类型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值