Shell脚本实例

Mar 30, 2016


fileopen命令实现

fileopen命令是为了实现Mac系统下,在终端中用应用打开文件。
首先将目录/Users/huanghuan/Work/MyCMD 命令加入环境变量中,Mac下可配置在/etc/bashrc中。然后创建fileopen文件,其内容为:

 1	#! /bin/sh
 2	if [ $# != 2 ]
 3	then
 4	    echo "输入参数错误!"
 5	else
 6	   search_result=`find /Applications -maxdepth 1  -name *${1}*.app | wc -l`
 7	   app_list=`find /Applications -maxdepth 1  -name *${1}*.app`
 8	   if (($search_result<=0))
 9	   then
10	       echo "未找到对应程序"
11	   elif (($search_result==1))
12	   then
13	       echo "打开文件"
14	       open ${app_list} ${2}
15	   else
16	       echo "符合条件的程序有:\n${app_list}"
17	       echo "请重新输入正确的程序名"
18	   fi
19	fi
20	exit

命令的运行效果如下:

fileopen命令

PS:该命令将进一步完善,目前作为一个Shell练习而简单实现

todo命令实现

todo命令实际上就是实现了一个toDoList的小命令,配置如fileopen命令,其内容为:

 1	#! /bin/sh
 2
 3	path="/Users/huanghuan/Work/MyCMD/toDoList"
 4
 5	while getopts a:r: option
 6	do
 7	  case $option in
 8	       a)
 9	         echo ${OPTARG}" 创建时间:"`date +%Y年-%m月-%d日` >>$path
10	         ;;
11	       r)
12	         if [ -f "$path" ]
13	         then
14	             gsed -i "${OPTARG}d" $path
15	         fi
16	         ;;
17	       ?)
18	         echo "Usage: toDo [-arp] [arg]"
19	         echo "-a add todo item"
20	         echo "-r remove todo item"
21	         echo "-p print todo List"
22	         exit
23	         ;;
24	  esac
25	done
26
27	echo "当前待办项目:"
28	if [ -f "$path" ]
29	then
30	    cat -n $path
31	fi
32	exit

命令的运行效果如下:

todo命令

该命令的扩展参见:todo扩展

Valid Phone Numbers

来源于Leetcode题目:Valid Phone Numbers

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.

You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)

You may also assume each line in the text file must not contain leading or trailing white spaces.

For example, assume that file.txt has the following content:

987-123-4567
123 456 7890
(123) 456-7890

Your script should output the following valid phone numbers:

987-123-4567
(123) 456-7890

脚本代码:

grep -E '^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$' file.txt

使用-E可以少写很多转义字符,这道题还值得注意的是:what’s the difference between \d and [0-9] in grep

Transpose File

来源于Leetcode题目:Transpose File

Given a text file file.txt, transpose its content.

You may assume that each row has the same number of columns and each field is separated by the ‘ ‘ character.

For example, if file.txt has the following content:

name age
alice 21
ryan 30

Output the following:

name alice ryan
age 21 30

脚本代码:

awk -F ' ' 'BEGIN{line=0;column=1} {while(column<=NF){array[line,column]=$column; column++;} line++;column=1;} END{for(i=1;i<=NF;i++){for(j=0;j<line;j++){printf("%s",array[j,i]);if(j<line-1){printf(" ")}} printf("\n");}}' file.txt

Word Frequency

来源于Leetcode题目:Word Frequency

Write a bash script to calculate the frequency of each word in a text file words.txt.

For simplicity sake, you may assume:

words.txt contains only lowercase characters and space ‘ ‘ characters. Each word must consist of lowercase characters only. Words are separated by one or more whitespace characters. For example, assume that words.txt has the following content:

the day is sunny the the
the sunny is is

Your script should output the following, sorted by descending frequency:

the 4
is 3
sunny 2
day 1

Note: Don’t worry about handling ties, it is guaranteed that each word’s frequency count is unique.

脚本代码:

sed 's/ /\n/g' words.txt | sed '/^$/d' | sort | uniq -c | sort -nr | awk -F ' ' '{print $2" "$1}'

上一篇博客:算法练习
下一篇博客:算法导论学习