if [[ $# -gt 1 ]]; then echo"Usage: $0 [dirname]" exit 1 elif [[ $# -eq 1 ]]; then cd"$@" # if cd command failed because it is not a valid path if [[ $? -ne 0 ]]; then exit 1 fi fi
# shell will extend '*' as all files and directories under the current directory for file in *; do if [[ -d "$file" ]]; then size=$(ls "$file" | wc -l | sed 's/[^[:digit:]]//g') echo"$file ($size entr(y|ies))" else size=$(ls -sk $file | awk '{print $1}') echo"$file (${size}KB)" fi done | \ sed -n \ -e '$!N' \ -E -e 's/\n/\x0/g' \ -e 'p' | \ awk -F "\0"'{ printf "%-39s %-39s\n",$1,$2 }'
### a bettern output fotmat
## step 1: use sed to make things easy and right # -n disable automatic priting # '$!N' append the next line of input into the pattern space, now we have two lines in sed's pattern space # 's/\n/\x0/g' replace '\n' in pattern space, so we make two lines a line separated by '\0' # 'p' print the content of pattern space
### step 2: awk final output # make the '\0' as field delimiter, and format the output
if [ $# -eq 0 ]; then echo"enter note and end with ctrl-D:"# ctrl-D Terminate input, or exit shell 终止输入或退出 shell cat - >>$remindFile else echo"$@" >>$remindFile fi
exit 0
查询提醒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env bash # remindMe 查看提醒
remindFile="$HOME/.remind"
if [ ! -f $remindFile ]; then echo"$0: You don't have a .remind file under your user home" >/dev/stderr exit 1 fi
if [ $# -eq 0 ]; then more $remindFile else grep -i -- "$@"$remindFile | ${PAGER:-more}# 变量为空默认赋值,即使用 more 命令来展示内容;-- 将后面参数不再作为 grep 命令的参数来解析,出于安全执行命令考虑 fi
functionshow_help() { cat <<EOF This is a help documentation, supported commands are as follows: (1) del del an item (2) add add a new item (3) mov move a item EOF }
echo"enter 'help' for help, 'quit' to quit" echo -n "calc> "