0

这里我有一个数据库 msmdb,其中包含一个表 emp。我正在尝试使用 bash 脚本显示该表中的值,但出现一个错误,提示找不到命令(在回显后的第 8 行)。

USER_NAME=root    
USER_PWD=prj    
DB_NAME=msmdb

flag=0     
delimiter="a3f4g52"

echo "select id ,'"$delimiter"', name,'"$delimiter"', city, '"$delimiter"', salary FROM emp" | \ 

mysql -u $USER_NAME --password=$USER_PWD --database=$DB_NAME | \ 

while read row ; 
do

   if [ $flag == 0 ]; then 
      flag=1 

   else 
      name=` echo $row | \ 
             awk -F "$delimiter" '{print $1}' | \ 
             sed -e 's/ *$//' | sed 's/^ *//'` 

      age=` echo $row | \ 
            awk -F "$delimiter" '{print $2}' | \ 
            sed -e 's/ *$//' | sed 's/^ *//'` 

      bornPlace=`echo $row | \ 
                 awk -F "$delimiter" '{print $3}' | \ 
                 sed -e 's/ *$//' | sed 's/^ *//'` 

      echo ">$name<"
      echo ">$age<"
      echo ">$bornPlace<"

   fi 
done

任何想法可能是错误的原因?

4

2 回答 2

0

Got The Script working properly ......

Just had to remove ' \ ' from the program and it worked.

The Correct bash script is

USER_NAME=root 
USER_PWD=prj
DB_NAME=msmdb 
flag=0 
delimiter="a3f4g52" 

echo "select id ,'"$delimiter"', name,'"$delimiter"', city, '"$delimiter"', salary FROM emp" |
mysql -u $USER_NAME --password=$USER_PWD --database=$DB_NAME |  

while read row ; 
do 
   if [ $flag == 0 ]; then 
      flag=1 
else 
      name=` echo $row | awk -F "$delimiter" '{print $1}' | sed -e 's/ *$//' | sed 's/^ *//'` 
      age=` echo $row | awk -F "$delimiter" '{print $2}' | sed -e 's/ *$//' | sed 's/^ *//'` 
      bornPlace=` echo $row | awk -F "$delimiter" '{print $3}' | sed -e 's/ *$//' | sed 's/^ *//'` 

   echo ">$name<" 
   echo ">$age<" 
   echo ">$bornPlace<" 
fi 
done 
于 2013-11-06T18:17:28.490 回答
0

在第 8 行中,换行符 (\) 后面有一个空格,因此换行符不起作用。

于 2013-11-06T17:34:39.233 回答