别名就是一种便捷方式,能够为用户省去输入一长串命令序列的麻烦。下面咱们会看到如何
使用 alias 命令建立别名。
直接使用alias就是显示当前有哪些别名,不然就是建立别名node
[root@dns-node2 ~]# alias # 显示别名 alias cp='cp -i' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' [root@dns-node2 ~]# alias mycmd='ls /root' # 建立别名 [root@dns-node2 ~]# mycmd anaconda-ks.cfg Desktop Documents Downloads install.log
在命令行建立别名是暂时的,一旦关闭当前这个终端,设置过的别名就失效了,为了永久生效,咱们须要导入到/etc/profile 或者~/.bashrc下shell
[root@dns-node2 ~]# echo "alias mycmd='ls /root'" >>/root/.bashrc
想要破解别名的话,就使用\来转义
举个例子:bash
[root@dns-node2 ~]# \mycmd -bash: mycmd: command not found [root@dns-node2 ~]# \ls # 非别名是无效的即便加上\ anaconda-ks.cfg Desktop Documents Downloads install.log install.log.syslog Music ossec-hids-2.8.3 ossec-hids-2.8.3.tar.gz Pictures Public Templates Videos [root@dns-node2 ~]# \ll -bash: ll: command not found
编写命令行shell脚本时,老是免不了处理当前终端的相关信息,好比行数、列数、光标位置、
遮盖的密码字段等。
tput 和 stty 是两款不错的工具ide
1.获取终端的行数和列数:工具
tput cols tput lines
2.打印出当前的终端名:命令行
tput longname
3.将光标移动到坐标(100,100)处:code
tput cup 100 100
4.设置终端背景色:dns
tput setb n
其中, n 能够在0到7之间取值。
5.设置终端前景色:cmd
tput setf n
其中, n 能够在0到7之间取值。file
6.设置文本样式为粗体:
tput bold
7.设置下划线的起止:
tput smul tput rmul
8.删除从当前光标位置到行尾的全部内容:
tput ed
9.输入密码时,脚本不该该显示输入内容。在下面的例子中,咱们将看到如何使用 stty 来
实现这一需求:
stty -echo # 此时隐藏全部的输入 stty echo # 显示全部的
9.1.具体参考的例子:
#!/bin/sh #Filename: password.sh echo -e "Enter password: " # 在读取密码前禁止回显 stty -echo read password # 从新容许回显 stty echo echo echo Password read
#!/bin/bash #文件名: sleep.sh echo 倒计时10秒 echo tput sc # 存储光标的位置 # 循环40秒 for count in {10..0} do tput rc # 恢复以前存储的光标的位置 tput ed # 清除从当前光标位置到行尾之间的全部内容,行被清空以后,脚本就能够显示出新的值。 echo -n "倒计时 $count" sleep 1 done