shell脚本输入


shell脚本输入|输出|管道


shell 开头

  • 以sh后缀开头
  • #注释,表明采用何种解释器
<code> #!/bin/bash /<code>
  • 添加作者等说明
<code> #/bin/bash

文件权限

<code>$ ll 
total 4
-rw-r--r-- 1 root root 88 Apr 24 22:15 demo.sh/<code>
  • 其中可以看到,demo.sh没有可执行权限;但是 bash demo.sh可以运行
<code> $ ./demo.sh
 -bash: ./demo.sh: Permission denied

 $ bash ./demo.sh
 shell demo/<code>
  • 也可以通过chmod 修改权限
<code>$ chmod 755 ./demo.sh 
# or
$ chmod u+x ./demo.sh  # 给当前拥有者添加可执行权限
$ ll 
-rwxr--r-- 1 root root 88 Apr 24 22:15 demo.sh/<code>

shell 输出 - echo

  • echo 输出特殊字符
<code> $ echo -e "hello\tworld"
 $ echo -e "hello\nworld"
 $ echo -e "hello\fworld"/<code>
  • 输出标色字符串
<code> # \e[35m开启,\e[0m关闭 
 $ echo -e "\e[35mok\e[0m"
 $ echo -e "\e[32mOK\e[0m"/<code>

shell - 输出 printf

<code>$ printf "%5d" 12 # 十进制右对齐输出,但是默认不换行
$ printf "%-5d" 12 # 十进制左对齐输出/<code>

shell - 输入 read

<code>$ read name  #把输入的值,赋值给name
$ echo $name 

$ read name age address # 读入3组
$ echo $name $age $address

$ read -p "请输入用户名" user
$ echo $user

$ read -t -10 -p "请输入用户名" user # 3s后自动退出等待输入状态
$ echo $user

$ read -s -p "请输入密码" pass # 不显示方式输入密码/<code>

shell - 管道|

<code> * who查看最近登录用户
 * wc 统计输入数据的行数-l,单词数 -w, 字节-c,字符-m
 $ who | wc -l
 * ss 可查看系统服务监听的端口,grep 具备过滤功能
 $ ss -nutlp | grep sshd/<code>


分享到:


相關文章: