내 쉘 스크립트에는 다음 줄이 있습니다.
function tarIt() {
FILE_OWNER=$1
TAR_FILE_NAME=$2
FILE_NAME=$3
su - $FILE_OWNER -c "tar -czf $TAR_FILE_NAME $FILE_NAME 2>&1"
if [ ${?} -ne 0 ]; then
print_error "Failed to create backup.";
exit $E_FAILED_COMPRESS;
fi
}
디버깅하는 동안 다음 줄이 표시됩니다.
+ su - ssrun -c 'tar -cvzf /tmp/backup/statusService/probe.properties.tar.gz /opt/mgtservices/probes/conf/probe.properties 2>&1'
Password: ssrun1
su: incorrect password
+ '[' 125 -ne 0 ']'
+ print_error 'Failed to create backup.'
비밀번호를 입력하라는 메시지가 표시될 때 내 텍스트가 표시되고 비밀번호를 가져오지 못하는 이유를 모르겠습니다. 비밀번호가 정확하고 명령 프롬프트에서 동일한 줄을 복사하면 오류 없이 실행됩니다.
아래는 업로드된 스크립트입니다. 여기에는 다른 스크립트에서 호출하는 유틸리티 메서드가 있습니다.
#!/bin/bash
# Exit Status
E_DIR_NOT_EXIST=55; # error if directory does not exist
E_DIR_PERMISSION_DENIED=77; # error if directory does not exist
if [ -z "${V9UPGRADE_LOG}" ]
then
LOGFILE=/tmp/${SCRIPTNAME}.$(date +'%Y%m%d_%H%M%S').log;
else
LOGFILE=${V9UPGRADE_LOG};
fi
# Verifies the run user or root user
if ! [[ $(whoami) =~ ^(ssrun|root)$ ]]
then
print_error "You have to be user ssrun or root to perform this operation.";
exit ${E_USER};
fi
# Function to log information
function print_info() {
format="$1"
shift
printf "$(date +'%F %T') ${SCRIPTNAME}: INFO: $format\n" "$@" | tee -a ${LOGFILE};
}
# Function to log warning
function print_warn() {
format="$1"
shift
printf "$(date +'%F %T') ${SCRIPTNAME}: WARN: $format\n" "$@" | tee -a ${LOGFILE};
}
# Function to log error
function print_error() {
format="$1"
shift
printf "$(date +'%F %T') ${SCRIPTNAME}: ERROR: $format\n" "$@" | tee -a ${LOGFILE} >&2;
}
function validateDir4Permission() {
DIR=$1
[ -d $DIR ] || { print_error "Invalid directory $DIR"; exit $E_DIR_NOT_EXIST; }
( su - $RUNUSR -c "[[ -rwx $DIR ]]" ) || { print_error "$DIR has insufficient permissions for ssrun user"; exit $E_DIR_PERMISSION_DENIED; }
( su - $ESYUSR -c "[[ -rwx $DIR ]]" ) || { print_error "$DIR has insufficient permissions for ne3suser user"; exit $E_DIR_PERMISSION_DENIED; }
[[ -rwx $DIR ]] || { print_error "$DIR has insufficient permissions for current user"; exit $E_DIR_PERMISSION_DENIED; }
}
# Function to append ".tar.gz" to name of file
function getArchiveFileName() {
echo ${SS_BACKUP_DIR}/${1}.tar.gz
}
# Function to compress the files and store in backup directory.
function tarIt() {
FILE_NAME=$1
TAR_FILE_NAME=$(getArchiveFileName $2)
FILE_OWNER=$3
if [[ -f $TAR_FILE_NAME ]]; then
print_info "$TAR_FILE_NAME already exists. Removing current instance.";
rm -f $TAR_FILE_NAME;
fi
su - $FILE_OWNER -c "tar -zcf $TAR_FILE_NAME $FILE_NAME > 2>&1"
if [ $? -ne 0 ]; then
print_error "Failed to compress $TAR_FILE_NAME."
exit $E_FAILED_COMPRESS;
else
if [[ -f $FILE_NAME ]]; then
print_info "File %s is backed up at %s" "$FILE_NAME" "$SS_BACKUP_DIR"
else
print_info "$All the contents of directory %s are compressed to %s." "$FILE_NAME" "$TAR_FILE_NAME"
fi
fi
}