1,自定义命令行快捷方式
通过使用**alias**命令实现自定义命令行快捷命令。你可以在命令行输入alias查看你有哪些自定义的快捷命令。
$ alias
>>
alias Lr='sh /home/simileciwh/catkin_new/src/multiagents_pathplanning/launch/include/load_multiple_robots/load_multiple_robots.sh'
alias Pg='sh /home/simileciwh/catkin_new/src/multiagents_pathplanning/launch/include/pub_robots_goals/pub_robots_goals.sh'
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias cnw='cd ~/catkin_new'
alias cw='cd ~/catkin_ws'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias g='gedit'
alias gb='gedit ~/.bashrc'
alias grep='grep --color=auto'
alias instal='sudo apt-get install'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias music='sudo netease-cloud-music'
alias ports='netstat -tulpn'
alias pycharm='cd /opt/pycharm-professional-2018.1.4/pycharm-2018.1.4/bin && ./pycharm.sh'
alias saria2='sudo aria2c --conf-path=/etc/aria2/aria2.conf -D'
alias sb='source ~/.bashrc'
alias update='sudo apt-get update'
alias v='vim'
alias vb='vim ~/.bashrc'
alias vip='sudo vim /etc/hosts'
alias vpnconnect='sudo openconnect -u simileci --script=/etc/vpnc/vpnc-script --no-dtls eu1.link.ac.cn'
alias vv='vim ~/.vimrc'
这是我的ubuntu上的,我自己的一些自定义的命令行快捷命令,每个人都是不同的,你可以参考我的设置。
举个栗子:
alias v='vim'
注意:等号两边不要加空格!!!
这个含义是,当你在命令行输入v时,系统会自动在后台解释成vim执行,这样可以减少很多输入单词的时间。比如执行命令:
sudo apt-get update
#可以设置成alias
alias update='sudo apt-get update'
1.1 alias的设置在哪里生效
如果想要你的命令行中,这些快捷的自定义命令永远生效,你需要将他们添加到你的 .bashrc 文件中,
vim ~/.bashrc
就可以打开隐藏文件bsahrc,我使用的是vim打开,你也可以使用gedit或者其他编译器打开。
然后在末尾,加入你自定义的 alias ,然后保存退出,要使得修改生效,你可以关闭终端重新打开,或者执行
source ~/.bashrc
使其立即生效。
1.2 自定义自己的bash配置
**.bashrc文件是$HOME**目录下隐藏的文件,可以通过键盘组合按键CTRL+H,显示隐藏的文件。bashrc文件是bash的配置文件,可以在bashrc文件中配置bash的环境。这样可以全局共享,当然也可以在单个bash文件中配置单独的环境。
1.2.1 shell 的配置选项
你可以在命令行,通过命令
set -o
查看所有的当前配置的shell选项。
$ set -o
>>
allexport off
braceexpand on
emacs on
errexit off
errtrace off
functrace off
hashall on
histexpand on
history on
ignoreeof off
interactive-comments on
keyword off
monitor on
noclobber off
noexec off
noglob off
nolog off
notify off
nounset off
onecmd off
physical off
pipefail off
posix off
privileged off
verbose off
vi off
xtrace off
你可以通过命令使能和关闭指定的功能
set +o variableName #取消shell选项的某一个功能
set -o variableName #使能shell选项的某一个功能
举个栗子:
在命令行中,默认可以通过组合按键CTRL+D关闭当前命令窗口的,通过上面的列举的shell选项可知ignoreeof是关闭的,默认是可以使用CTRL+D,退出终端的。现在禁止这个功能,
set -o ignoreeof
此时,在命令行中再次使用CTRL+D,会输出如下信息:
Use “exit” to leave the shell.
命令行没有退出。
再次使能这个功能:
set +o ignoreeof
就可以再次使用CTRL+D来退出命令行了。
其他功能和这个一样。
如果你像设置在所有的命令行都一样,就把你的设置写在 bashrc 文件中就行了。
1.2.2 控制变量选项行为值的命令
使用 shopt 命令,查看当前bash shell的配置选项:
$ shopt
>>
autocd off
cdable_vars off
cdspell on
checkhash off
checkjobs off
checkwinsize on
cmdhist on
compat31 off
compat32 off
compat40 off
compat41 off
compat42 off
compat43 off
complete_fullquote on
direxpand off
dirspell off
dotglob off
execfail off
expand_aliases on
extdebug off
extglob on
extquote on
failglob off
force_fignore on
globasciiranges off
globstar off
gnu_errfmt off
histappend on
histreedit off
histverify off
hostcomplete off
huponexit off
inherit_errexit off
interactive_comments on
lastpipe off
lithist off
login_shell off
mailwarn off
no_empty_cmd_completion off
nocaseglob off
nocasematch off
nullglob off
progcomp on
promptvars on
restricted_shell off
shift_verbose off
sourcepath on
xpg_echo off
你可以通过指令使能或者关闭某一个功能选项
shopt -s optionName #使能设置的某一个选项
shopt -u optionName #取消设置的某一个选项
举个栗子:
以shopt中的一项选项为例,cdspell ,是模糊化输入的命令,比如一个文件夹名字你输错了2个字符,那么 cd 仍然可以找到这个文件夹,但是如果你连续输错3个字符,那么几乎就找不到了!
$ shopt -u cdspell
$ cd /etcc
>>
bash: cd: /etcc: No such file or directory
$shopt -s cdspell
$cd /etcc
>>
/etc
#然后光标就进入了根目录下的etc目录,即/etc
当你查看完set和shopt的选项之后,选择对你有用的命令,写到 .bashrc 文件中,source ~/.bashrc 即可使用。
2,test命令的使用
test命令是用来核实文件类型和比较值的。主要用来:
- 文件属性比较;
- 算数比较;
- 执行字符串比较
"语法":
test condition
#或者
test condition && true-command
#或者
test condition || false-commond
#或者
test condition && true-commond || false-commond
举个栗子:
test 5 > 2 && echo Yes
test 5 < 2 && echo Yes || echo No
test 5 -lt 2 && echo Yes || echo No
执行结果:
Yes
Yes
Yes
这里的 -lt 是lower than的意思,即 < 号。这里test在执行直接的数学云算法总是会出现bug,但是类似 -lt 这样的命令可以正常处理。
举个栗子:
test 5 < 2 && echo Yes || echo No
test 5 > 2 && echo Yes || echo No
执行结果:
Yes
Yes
总是执行判断正确的语句,这很奇怪。
但是,数学逻辑没有问题。
echo $((5 > 2))
>>
1
echo $((5 < 2))
>>
0
3,if 结构的使用
3.1 if语句的语法
"语法":
if 条件
then./begin09.sh
Please input your age, i will tell you whether you can drive a car! age: 90\
you can drive a car!
cmd1
cmd2
...
cmd3
fi
或者 if-else
if 条件
then
cmd1
cmd2
else
cmd3
cmd4
fi
或者多个 if-then-else
if 条件1
then<font color=lightcoral>**举个栗子:**</font>---- ==**if-else**==
cmd1
cmd2
elif 条件2
then
cmd3
cmd4
elif 条件3
then
cmd5
cmd6
else
cmd7
fi
举个栗子:---- if-else
read -p "input two integer number a,b" a b
if [ $((a<=b)) -eq 1 ]
then
echo -e "$a <= $b \n"
else
echo -e "$a > $b \n"
fi
执行结果:
input two integer number a,b 5 10
5 <= 10
input two integer number a,b 13 -2
13 > -2
## 3.2 if 语句的嵌套 **"语法":**
if 条件1
then
if 条件2
then
...
...
do this
else
...
...
do this
fi
else
...
...
do this
fi
**举个栗子:**
#! /bin/bash
#__Filename__ : begin09.sh
#__Auther__ : simileciwh
#Email : simileci.wh.32@outlook.com
#Created date : 2018-11-23 14:48:09
# code is far away from bugs with the god animal protecting
# I love animals. I have a lovely doggy!
# ┏┓ ┏┓
# ┏┛┻━━━━━┛┻┓
# ┃ ☃ ┃
# ┃ ┳┛ ┗┳ ┃
# ┃ ┻ ┃
# ┗━┓ ┏━┛
# ┃ ┗━━━┓
# ┃ 神兽保佑 ┣┓
# ┃ 永无BUG! ┏┛
# ┗┓┓┏━━┳┓┏┛
# ┃┫┫ ┃┫┫
# ┗┻┛ ┗┻┛
read -p "Please input your age, i will tell you whether you can drive a car! age: " age
if [ ${age} -ge 18 ]
then
echo "you can drive a car!"
else
if [ ${age} -lt 10 ]
then
echo "your first thing is to grow up healthy!"
else
echo "it is too early to learn skills of drive a car!"
fifei
fi
执行结果:
$ ./begin09.sh
>>
Please input your age, i will tell you whether you can drive a car! age: 17
it is too early to learn skills of drive a car!
$ ./begin09.sh
>>
Please input your age, i will tell you whether you can drive a car! age: 3
your first thing is to grow up healthy!
$ ./begin09.sh
>>
Please input your age, i will tell you whether you can drive a car! age: 90
you can drive a car!
注意:在 “[” "]"的前后需要留有空格,不然会报错
4,命令行的退出状态
在linux系统下,不管终端是正常运行还是异常结束,都会返回一个状态值,你可以访问这个退出状态值,来确定之前执行的命令是否正常完成或者出现错误。这个反馈的功能非常有用。
如何知道命令行的退出状态呢?
可以通过 ? 来取得命令行退出状态值。
举个栗子:
你在命令行输入
$ date
>>
2018年 11月 30日 星期五 11:14:44 CST
$ echo $?
>>
0
$ date1
>>
Command 'date1' not found, did you mean:
command 'date' from deb coreutils
Try: sudo apt install <deb name>
$ echo $?
>>
127
? 中的 值为0 ,则代表前一个命令的执行是正常的, 不为0 则代表前一个命令执行异常。
5,逻辑与(&&),逻辑或(||)和逻辑非(!)
"语法":
cmd1 && cmd2
只有当cmd1执行成功时,才会执行cmd2!
cmd1 || cmd2
只有当cmd1执行失败时,才会执行cmd2!
!表达式
一般用来测试表达式是真还是假!
6,条件表达式使用 “[”
"语法":
[ condition ]
#或者
[ ! condition ]
#或者
[ condition ] || false-cmd
#或者
[ condition ] && true-cmd || false-cmd
举个栗子:
[ 5 == 5 ] && echo "Yes" || echo "No"
[ 5 == 15 ] && echo "Yes" || echo "No"
[ 5 != 10 ] && echo "Yes" || echo "No"
[ -f /etc/resolv.conf ] && echo "File /etc/resolv.conf found." || echo
"File /etc/resolv.conf not found."
[ -f /etc/resolv1.conf ] && echo "File /etc/resolv.conf found." || echo
"File /etc/resolv.conf not found."
执行结果:
Yes
No
Yes
File /etc/resolv.conf not found.
File /etc/resolv.conf not found.
7,数学比较符
运算符 | 语法 | 描述 |
---|---|---|
eq(=) | int1 -eq int2 | int1 == int2 |
ge(>=) | int1 -ge int2 | int1 >= int2 |
gt(>) | int1 -gt int2 | int1 > int2 |
le(<=) | int1 -le int2 | int1 <= int2 |
lt(<) | int1 -lt int2 | int1 < int2 |
ne(!=) | int1 -ne int2 | int1 != int2 |
8,字符串比较
字符串的比较可以使用test命令
"语法":
string1 = string2
举个栗子:
read -s -p "input your passward" password
echo
if test "${password}" != "simileciwh"; then
echo "wrong password!"
else
echo "Hello, to log in!"
fi
当然你也可以不加双引号,直接比较,不过看着不太符合标准,但是不存在错误。
if test ${password} != simileciwh; then
注意:在 "="的前后需要留有空格,不然会报错
执行结果:
#随便输入一个错误的密码,或者直接回车。
>>
input your passward
wrong password!
#输入一个正确的密码。
>>
input your passward
Hello, to log in!
8.1 检查字符串是否为空字符串
使用以下的语法可以检查一个字符串是否存放空字符。
"语法":
-z string
举个栗子:
read -s -p "input your passward" password
echo
if test -z $password; then
echo "your input is empty!"
else
if test "${password}" != "simileciwh"; then
echo "wrong password!"
else
echo "Hello, to log in!"
fi
fi
执行结果:
$ ./begin09.sh
>>
#如果什么都不输入直接回车,则输出:
input your passward
your input is empty!
9,文件属性比较
详细请看下面的表格,注意表格中的"file"为文件所在位置包括文件名和类型。,比如,$HOME/documents/123.txt.
格式 | 含义 |
---|---|
-a file | 如果文件存在则为true |
-b file | 如果文件存在并且是一个 bolck的 特殊文件,则为true |
-c file | 如果文件存在并且是一个 character类型的特殊文件,则为true |
-d file | 如果文件存在并且是一个 目录,则为true |
-e file | 如果文件存在则为true |
-f file | 如果文件存在并且是一个普通文件,则为true |
-g file | 如果文件存在,并且设置了用户组名,则为true |
-h file | 如果文件存在,并且是一个象征性的链接,则为真 |
-k file | 如果文件存在,并且它的sticky位被设置了,则为true |
-p file | 如果文件存在,并且是一种pipe的命名(FIFO),则为true |
-r file | 如果文件存在,并且是只读的,则为true |
-s file | 如果文件存在,并且非空,则为true |
-t fd | 如果文件描述的fd是打开的,并且是在终端中打开的,则为true |
-u file | 如果文件存在,并且用户组的ID位被设置了,则为true |
-w file | 如果文件存在,并且是可写的,则为true |
-x file | 如果文件存在,并且是可执行的文件,则为true |
这写在确定文件的属性时都非常的有用处。