在shell中用特殊字符构建一个字符串

时间:2015-12-22 04:57:45

标签: shell unix

我想建立一个如下命令

sqoop export --connect jdbc:db2://xx.xx.xx.xxx:50000/DB1 --username user1ib2c --password pass123! --table table1 --export-dir /user/hduser 

我最初在变量中以小部分存储了东西,然后尝试在文件中合并如下

sqoop_cmd1="sqoop export --connect $target_db_url"
sqoop_cmd2="--username $db_user_nm"
sqoop_cmd3="--password $db_password"
sqoop_cmd4="--table $tbl_nm_upr --export-dir $export_dir"

`echo $sqoop_cmd1` > $sqoop_cmd
`echo $sqoop_cmd2` >> $sqoop_cmd
`echo $sqoop_cmd3` >> $sqoop_cmd
`echo $sqoop_cmd4` >> $sqoop_cmd

但得到如下错误

script1.sh: line 77: --username: command not found
script1.sh: line 78: --password: command not found
script1.sh: line 79: --table: command not found

我正在使用的shell脚本:



#!/usr/bin/ksh

loaddate=$(date +"%Y-%m-%d %H:%M:%S")
date_val=$(date +"%Y%m%d_%H%M%S")
RETURN_CODE=0
param_file=$1
tablename=$2

. $1

printf "Execution Started at: `date` \n"

if [ "$#" -ne 2 ]; then
   printf "At least two arguments are required"
   exit 1
fi

tbl_nm_lwr=`echo $tablename | tr '[:upper:]' '[:lower:]'`
printf "\n Lowercase table name = $tbl_nm_lwr \n"

tbl_nm_upr=`echo $tablename | tr '[:lower:]' '[:upper:]'`
printf "\n Uppercase table name = $tbl_nm_upr \n"

fnl_tbl=TEST.$tbl_nm_upr
printf "\n tablename = $fnl_tbl \n"

#==========================================================================
# Functionality to determine the execution time
#==========================================================================

ST_TIME=`date +%s`

# create a zero byte file to confirm execution started
touch data_copy_to_ioc.zerobyte

sqoop_cmd=$tbl_nm_lwr"_sqoop"
sqoop_cmd1=$tbl_nm_lwr"_sqoop1"
sqoop_cmd2=$tbl_nm_lwr"_sqoop2"
sqoop_cmd3=$tbl_nm_lwr"_sqoop3"
sqoop_cmd4=$tbl_nm_lwr"_sqoop4"

sqoop_cmd1="sqoop export --connect $target_db_url"
sqoop_cmd2="--username $db_user_nm"
sqoop_cmd3="--password $db_password"
sqoop_cmd4="--table $tbl_nm_upr --export-dir $export_dir"

echo -n " ${sqoop_cmd1}" > $sqoop_cmd
echo -n " ${sqoop_cmd2}" >> $sqoop_cmd
echo -n " ${sqoop_cmd3}" >> $sqoop_cmd
echo -n " ${sqoop_cmd4}" >> $sqoop_cmd

if [ $RETURN_CODE -eq 0 ]; then
    printf "\n Success: Table $tablename exported successfully at `date` \n"
else
    printf "\n Error: RETURN_CODE=$RETURN_CODE exported $tablename at `date`"
    exit $RETURN_CODE
fi

rm -f data_copy_to_ioc.zerobyte

RETURN_CODE=$?

if [ 0 -ne $RETURN_CODE ]
   then
       printf "\n Error: RETURN_CODE=$RETURN_CODE exported $tablename at `date`"
       exit $RETURN_CODE
   else
       printf "\n Success: Export completed for $tablename at `date` \n"
fi

#==========================================================================
# Calculate the time taken by the script
#==========================================================================
END_TIME=`date +%s`
TT_SECS=$(( END_TIME - ST_TIME))
TT_HRS=$(( TT_SECS / 3600 ))
TT_REM_MS=$(( TT_SECS % 3600 ))
TT_MINS=$(( TT_REM_MS / 60 ))
TT_REM_SECS=$(( TT_REM_MS % 60 ))
printf "\n"
printf "Total time taken to complete for $tbl_nm_lwr="$TT_HRS:$TT_MINS:$TT_REM_SECS HH:MM:SS
printf "\n"

exit $RETURN_CODE




尝试使用sh sqoopioc_koushik.sh /home/hduser/data_copytoioc_param.conf TABLE1

运行它

配置文件如下:



log_dir=/home/hduser/Logs
log_file=export_data_from_bigsql_to_DB2
export_dir=/home/hduser/export
hdfs_defult_dir=/tmp/$whoami
sqoop_dir=/usr/iop/4.0.0.0/sqoop/bin
target_db_ip=100.100.100.100
target_db_port=50000
target_db_nm=TESTDB
db_user_nm=user1
db_password=ChangeM3N0w\!
target_db_url=jdbc:db2://100.100.100.100:50000/TESTDB
target_db_schema=TEST
bigsql_user=bigsql
bigsql_password=pass123




1 个答案:

答案 0 :(得分:0)

定义常量值

target_db_url="jdbc:db2://xx.xx.xx.xxx:50000/DB1"
db_user_nm="user1ib2c"
db_password="pass123!"
tbl_nm_upr="table1"
export_dir="/user/hduser"

构建单独的字符串

sqoop_cmd1="sqoop export --connect $target_db_url"
sqoop_cmd2="--username $db_user_nm"
sqoop_cmd3="--password $db_password"
sqoop_cmd4="--table $tbl_nm_upr --export-dir $export_dir"

最终脚本文件名

sqoop_cmd="script1.sh"

使用echo" -n" (没有新行,因为默认情况下echo添加换行符)

echo -n " ${sqoop_cmd1}" > $sqoop_cmd
echo -n " ${sqoop_cmd2}" >> $sqoop_cmd
echo -n " ${sqoop_cmd3}" >> $sqoop_cmd
echo -n " ${sqoop_cmd4}" >> $sqoop_cmd