Shell脚本中使用变量引用

Posted by Harid七月 - 20 - 2014 Leave comments

Shell脚本中,有时会需要使用变量的间接引用来减少代码量,我们可以使用eval这个工具来实现变量的间接引用。

具体使用方法如下:

1
2
3
a=123
b=a
eval LAST=\$$b

这样,“LAST”这个变量的值即变成了123。

帖一个具体的shell脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
 
### Directory list here ###
DIR_NUM=15
USERNAME="*****"
DIR1="/home/*****/*****/error/splitter/nexus"
DIR2="/home/*****/*****/exception/flw_splitter"
DIR3="/home/*****/*****/error/nexus"
DIR4="/home/*****/*****/Error_*****/nexus/verify_err"
DIR5="/home/*****/*****/exception/flw_nexus"
DIR6="/home/*****/*****/error/splitter/tap"
DIR7="/home/*****/*****/exception/flw_splitter_tap"
DIR8="/home/*****/*****/error/tap"
DIR9="/home/*****/*****/Error_*****/tap/verify_err"
DIR10="/home/*****/*****/exception/flw_tap_voice_and_sms"
DIR11="/home/*****/*****/error/splitter/custlist"
DIR12="/home/*****/*****/exception/flw_splitter"
DIR13="/home/*****/*****/error/custlist"
DIR14="/home/*****/*****/Error_*****/custlist/verify_err"
DIR15="/home/*****/*****/exception/flw_custlist"
### End directory list ###
 
### Do not edit the follow area ###
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/local/sbin:/usr/sbin:$PATH
export PATH
 
LOG_DIR="`cat /etc/passwd | grep "^${USERNAME}:" | cut -d ':' -f6`"
LOG_FILE="${LOG_DIR}/checkFiles.log"
CURRENT_DIR=`pwd`
rm -f ${LOG_DIR}/checkFiles.log
function print_s(){
        echo "" | tee -a ${LOG_FILE}
        echo "   -----------------------------------------------" | tee -a ${LOG_FILE}
        echo "    $1" | tee -a ${LOG_FILE}
        echo "   -----------------------------------------------" | tee -a ${LOG_FILE}
        echo "" | tee -a ${LOG_FILE}
}
 
function get_char(){
	echo ""
	echo -e "   \033[32mPress any key to check if is there any new file occurred today in these directories, CTRL+C to quit...\033[0m"
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
}
 
if [ "X`cat /etc/passwd | grep "^${USERNAME}:" | cut -d ':' -f3`" != "X`id -u`" ];then
	clear
	print_s "You must execute this script as ${USERNAME} account. Exit now!"
	exit 1
fi
 
function test_dir_exist(){
	DIR_NAME=$1
	if [ "X${DIR_NAME}" == "X" ];then
		print_s "You must give the directory path for testing. Exit now!"
		exit 1
	fi
 
	if [ -d ${DIR_NAME} ];then
		return 0
	else
		return 1
	fi
}
 
function check_new_file(){
	DIR_NAME=$1
	if [ "X${DIR_NAME}" == "X" ];then
		print_s "You must give the directory path for testing. Exit now!"
                exit 1
	fi
 
	cd ${DIR_NAME} > /dev/null 2>&1
	if [ `echo $?` -eq 0 ];then
		if [ `ls -l --time-style=long-iso | grep "$(date +%F)" | wc -l` -gt 0 ];then
			return 1
		else
			return 0
		fi
	else
		return 2
	fi
}
 
function print_welcome(){
	clear
	echo -e "***********************************************************************************************" | tee -a ${LOG_FILE}
	printf "   \033[34m%-30s\033[0m\n" "Directories existence in testing list:" | tee -a ${LOG_FILE}
	printf "   \033[34m%-8s\033[0m\033[34m%-70s\033[0m\033[34m%-12s\033[0m\n" "Dir_No" "Directory_Path" "Is_Exist?"
	for ((i=1; i<=${DIR_NUM}; i=i+1))
	do
		DIR_list="DIR${i}"
		eval DIR_NAME=\$$DIR_list
		if [ "X${DIR_NAME}" != "X" ];then
			test_dir_exist ${DIR_NAME}
			if [ `echo $?` -eq 0 ];then
				printf "   %-8s%-70s\033[32m%-12s\033[0m\n" "DIR${i}" "${DIR_NAME}" "[Exist]" | tee -a ${LOG_FILE}
			else
				printf "   %-8s%-70s\033[31m%-12s\033[0m\n" "DIR${i}" "${DIR_NAME}" "[Not exist]" | tee -a ${LOG_FILE}
			fi
		else
			printf "   %-8s\033[31m%-82s\033[0m\n" "DIR${i}" "Directory path of ${DIR_list} is not given!" | tee -a ${LOG_FILE}
		fi
	done
	echo -e ""
	echo -e "************************************************************************************************" | tee -a ${LOG_FILE}
	echo ""
}
 
function print_check_result(){
        clear
        echo -e "***********************************************************************************************" | tee -a ${LOG_FILE}
        printf "   \033[34m%-30s\033[0m\n" "New file occurred today in testing list:" | tee -a ${LOG_FILE}
        printf "   \033[34m%-8s\033[0m\033[34m%-70s\033[0m\033[34m%-21s\033[0m\033[34m%-70s\033[0m\n" "Dir_No" "Directory_Path" "Is_New_File_Exist?" "Example_File"
        for ((i=1; i<=${DIR_NUM}; i=i+1))
        do
                DIR_list="DIR${i}"
                eval DIR_NAME=\$$DIR_list
                if [ "X${DIR_NAME}" != "X" ];then
                        test_dir_exist ${DIR_NAME}
                        if [ `echo $?` -eq 0 ];then
				check_new_file ${DIR_NAME}
				result=`echo $?`
				if [ ${result} -eq 0 ];then
	                                printf "   %-8s%-70s\033[32m%-19s\033[0m\n" "DIR${i}" "${DIR_NAME}" "[No]" | tee -a ${LOG_FILE}
				elif [ ${result} -eq 1 ];then
					cd ${DIR_NAME}
					new_file=`ls -l --time-style=long-iso | grep "$(date +%F)" | head -1 | awk '{print $8}'`
					cd ${CURRENT_DIR}
					printf "   %-8s%-70s\033[31m%-19s\033[0m\033[31m%-70s\033[0m\n" "DIR${i}" "${DIR_NAME}" "[Yes]" "${DIR_NAME}/${new_file}" | tee -a ${LOG_FILE}
				else
					printf "   %-8s\033[31m%-89s\033[0m" "DIR${i}" "No permission to access to ${DIR_NAME}" | tee -a ${LOG_FILE}
				fi
                        else
                                printf "   %-8s%-70s\033[31m%-19s\033[0m\n" "DIR${i}" "${DIR_NAME}" "[Not exist]" | tee -a ${LOG_FILE}
                        fi
                else
                        printf "   %-8s\033[31m%-82s\033[0m\n" "DIR${i}" "Directory path of ${DIR_list} is not given!" | tee -a ${LOG_FILE}
                fi
        done
        echo -e ""
        echo -e "************************************************************************************************" | tee -a ${LOG_FILE}
        echo ""
	echo "   Log file is ${LOG_FILE}"
	echo ""
}
 
print_welcome
get_char
print_check_result
 
exit 0

效果如下:

result1

result2

   声明:本文采用 BY-NC-SA 协议进行授权 | 星期九
   原创文章转载请注明:转自《Shell脚本中使用变量引用


分享按钮