realname.sh
来自「Shall高级编程」· Shell 代码 · 共 38 行
SH
38 行
#!/bin/bash# realname.sh## From username, gets "real name" from /etc/passwd.ARGCOUNT=1 # Expect one arg.E_WRONGARGS=65file=/etc/passwdpattern=$1if [ $# -ne "$ARGCOUNT" ]then echo "Usage: `basename $0` USERNAME" exit $E_WRONGARGSfi file_excerpt () # Scan file for pattern, then print relevant portion of line.{while read line # "while" does not necessarily need "[ condition ]"do echo "$line" | grep $1 | awk -F":" '{ print $5 }' # Have awk use ":" delimiter.done} <$file # Redirect into function's stdin.file_excerpt $pattern# Yes, this entire script could be reduced to# grep PATTERN /etc/passwd | awk -F":" '{ print $5 }'# or# awk -F: '/PATTERN/ {print $5}'# or# awk -F: '($1 == "username") { print $5 }' # real name from username# However, it might not be as instructive.exit 0
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?