Shell脚本中使用变量引用

Posted by Harid2014 - Jul - 20 留个言

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

具体使用方法如下:

a=123
b=a
eval LAST=\$$b

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

帖一个具体的shell脚本:

#!/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脚本中使用变量引用

No comments yet.
98 + 9 =  (required)
comment_ad

 NOTICE1: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!

 NOTICE2: 请申请gravatar头像(http://en.gravatar.com),木有头像的会显示为“小怪物”头像,将难以通过审核!

分享按钮