1. echo
[kiosk@foundation60 echo]$ cat 01.sh
#!/bin/bash
name='Tom'
age=20
height=180
weight=70
echo -n "${name} is ${age} years old," # -n:表示不换行
echo -n "${height}cm in height "
echo "and ${weight}kg in weight"
echo "Thank you!"
[kiosk@foundation60 echo]$ sh 01.sh
Tom is 20 years old,180cm in height and 70kg in weight
Thank you!
echo -e
与 特殊字符串结合处理字符串
echo -e 特殊字符 | 说明 |
---|---|
\a | 发出警告声 |
\b | 删除前一个字符 |
\c | 最后不加上换行符号 |
\f | 换行但光标仍旧停留在原来的位置 |
\n | 换行且光标移至行首 |
\r | 光标移至行首,但不换行 |
\t | 插入tab |
\v | 与\f相同 |
\ | 插入\字符 |
[kiosk@foundation60 echo]$ echo -e "hello \nworld"
hello
world
[kiosk@foundation60 echo]$ cat 02.sh
#!/bin/bash
name='Tom'
age=20
height=180
weight=70
# echo -e结合\c 强制echo命令不换行
echo -e "${name} is ${age} years old, \c"
echo -e "${height}cm in height \c"
echo "and ${weight}kg in weight"
echo "Thank you!"
[kiosk@foundation60 echo]$ sh 02.sh
Tom is 20 years old, 180cm in height and 70kg in weight
Thank you!
2. read
read的选项 | 用法 |
---|---|
-p | 用于给出提示符 |
-n | 用于限定最多可以有多少字符可以作为有效读入 |
-t | 用于表示等待输入的时间,单位为秒 |
-s | 默读(不显示在显示器上) |
[kiosk@foundation60 read]$ cat 01.sh
#!/bin/bash
# read -p 显示提示信息 input("str:")
# 注意:必须在一行内输入所有的值 不能换行
# 否则 只能给第一个变量赋值 后续变量赋值都会失败
read -p "Enter some information >" name url age
echo "网站的名字:$name"
echo "网址:$url"
echo "年龄:$age"
[kiosk@foundation60 read]$ sh 01.sh
Enter some information >zjy www.baidu.com 88
网站的名字:zjy
网址:www.baidu.com
年龄:88
[kiosk@foundation60 read]$ cat 02.sh
#!/bin/bash
# read -n num
# -n 1 表示只读取一个字符
read -n 1 -p "Enter a char >" char
printf "\n"
echo $char
[kiosk@foundation60 read]$ sh 02.sh
Enter a char >y
y
-t表示计时器;-s:默读(不显示在显示器上);&&左边的命令返回真后,&&右边的命令才能够被执行。
[kiosk@foundation60 read]$ cat 03.sh
#!/bin/bash
if
read -t 20 -sp "Enter password in 20 seconds(once) > " pass1 && echo -e "\n" &&
read -t 20 -sp "Enter password in 20 seconds(again)> " pass2 && echo -e "\n" &&
[ $pass1 == $pass2 ] #判断两次输入的密码是否相等
then
echo "Valid password"
else
echo "Invalid password"
fi
[kiosk@foundation60 read]$ sh 03.sh
Enter password in 20 seconds(once) >
Enter password in 20 seconds(again)>
Valid password
3. alias
使用alias命令自定义别名的语法格式为:alias new_name='command'
[kiosk@foundation60 read]$ alias timestap='date +%s'
[kiosk@foundation60 read]$ timestap
1580478892
[kiosk@foundation60 alias]$ cat 01.sh
#!/bin/bash
alias timestamp='date +%s'
begin=`timestamp`
sleep 10s
finish=$(timestamp)
differince=$((finish - begin))
echo "run time: ${differince}"
[kiosk@foundation60 alias]$ sh 01.sh
run time: 10