5
回答
菜鸟新手shell生成文件输出求助~~
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

网上折腾一天,东凑西拼的代码。。菜菜菜鸟级的,一直读取不了$key的变量。。。

想要读取key.txt里面内容,输出title.sql文件,里面内容实现成


UPDATE `testaaa`.`db_1_options` SET `option_value` = '克洛基石' WHERE `db_1_options`.`option_id` =2;
UPDATE `testaaa`.`db_2_options` SET `option_value` = '凡晶石' WHERE `db_2_options`.`option_id` =2;
UPDATE `testaaa`.`db_3_options` SET `option_value` = '同位原矿' WHERE `db_3_options`.`option_id` =2;
UPDATE `testaaa`.`db_4_options` SET `option_value` = '干焦岩' WHERE `db_4_options`.`option_id` =2;
UPDATE `testaaa`.`db_5_options` SET `option_value` = '灼烧岩' WHERE `db_5_options`.`option_id` =2;
UPDATE `testaaa`.`db_6_options` SET `option_value` = '相位变极器' WHERE `db_6_options`.`option_id` =2;
UPDATE `testaaa`.`db_7_options` SET `option_value` = '离子力场投射器' WHERE `db_7_options`.`option_id` =2;
UPDATE `testaaa`.`db_8_options` SET `option_value` = '空间扰乱器' WHERE `db_8_options`.`option_id` =2;
UPDATE `testaaa`.`db_9_options` SET `option_value` = '脉冲波' WHERE `db_9_options`.`option_id` =2;
UPDATE `testaaa`.`db_10_options` SET `option_value` = '停滞缠绕光束' WHERE `db_10_options`.`option_id` =2;


key.txt 内容


克洛基石
凡晶石
同位原矿
干焦岩
灼烧岩
相位变极器
离子力场投射器
空间扰乱器
脉冲波
停滞缠绕光束


x.sh 内容

#!/bin/bash

a_count=200
key=`wc-l <key.txt` seti=1 while($i <= $n) setline="`awk '{if (NR == $i) print}' key.txt`"'

URL="url.sql"
TITLE="title.sql"

[[ -f "$TXT" ]] && rm -f $TXT
 
for ((i=1;i<=$a_count;i++))
do
 
        matrix="0123456789abcdefghijklmnopqrstuvwxyz"
        length="8"
        while [ "${num:=1}" -le "$length" ];do
                code="$code${matrix:$(($RANDOM%${#matrix})):1}"
                let num+=1
        done
        echo "(${i}, 1, '${code}', '/', '2013-05-22 17:43:37', '2013-05-22 17:43:37', 1, '0', 0, 0, 0, 0)," >> $TXT
	echo "UPDATE \`testaa\`.\`db_${i}_options\` SET \`option_value\` = '${code}' WHERE \`wp_${i}_options\`.\`option_id\` =2;" >> $URL
        echo "UPDATE \`testaa\`.\`db_${i}_options\` SET \`option_value\` = '$key' WHERE \`wp_${i}_options\`.\`option_id\` =2;" >> $TITLE
done


举报
共有5个答案 最后回答: 5年前

你也写得太麻烦了,我都没看明白你想干什么!我来写过, 一行代码就能实现你的需求

#!/bin/bash

awk '{print "UPDATE `testaaa`.`db_"NR"_options` SET `option_value` = " "'\''" $0 "'\''" " WHERE `db_"NR"_options`.`option_id` = 2;"}' key.txt
[ping.bao@dinga temp]$ ./a.sh
UPDATE `testaaa`.`db_1_options` SET `option_value` = '克洛基石' WHERE `db_1_options`.`option_id` = 2;
UPDATE `testaaa`.`db_2_options` SET `option_value` = '凡晶石' WHERE `db_2_options`.`option_id` = 2;
UPDATE `testaaa`.`db_3_options` SET `option_value` = '同位原矿' WHERE `db_3_options`.`option_id` = 2;
UPDATE `testaaa`.`db_4_options` SET `option_value` = '干焦岩' WHERE `db_4_options`.`option_id` = 2;
UPDATE `testaaa`.`db_5_options` SET `option_value` = '灼烧岩' WHERE `db_5_options`.`option_id` = 2;
UPDATE `testaaa`.`db_6_options` SET `option_value` = '相位变极器' WHERE `db_6_options`.`option_id` = 2;
UPDATE `testaaa`.`db_7_options` SET `option_value` = '离子力场投射器' WHERE `db_7_options`.`option_id` = 2;
UPDATE `testaaa`.`db_8_options` SET `option_value` = '空间扰乱器' WHERE `db_8_options`.`option_id` = 2;
UPDATE `testaaa`.`db_9_options` SET `option_value` = '脉冲波' WHERE `db_9_options`.`option_id` = 2;
UPDATE `testaaa`.`db_10_options` SET `option_value` = '停滞缠绕光束' WHERE `db_10_options`.`option_id` = 2;
顶部