多个for循环

Multiple for loop

我正在尝试编写修改文件的脚本。 我想检查文件中定义的文本,如果存在则更改它,如果不添加它。 要更改的文本在变量中定义:

line1=text string 1
line2=text string 2
line3=text string 3
# new values
line1Arg=value 1
line2Arg=value 2
line3Arg=value 3

到目前为止我有以下内容:

for i in $line1 $line2 $line3;
do
oldarg=`grep $i $file |cut -d"=" -f2`;
chkexists=`grep -c $i $file`
if [ $chkexists -eq 0 ]; then
cp $file $tmpfile

这就是我卡住的地方... 他对变量 $linexArg 进行循环并将其添加到 $tmpfile?

echo $i='new value' >> $tmpfile;
echo "$i added with newvalue";
else
echo "$i exists with $oldarg";

可能很简单,但我是脚本新手!

编辑 1:

感谢 Matt,我现在有了这个,它只从 $line1-5 添加了 1 行,它应该添加 2 个缺失的行,$line4 和 $line5

file=/folder/file
tmpfile=$file.tmp

#  strings to find and modify / add
line1=text string 1
line2=text string 2
line3=text string 3
line4=text string 4
line5=text string 5
# new values
line1Arg=120
line2Arg=370
line3Arg=100
line4Arg=999
line5Arg=1000

for i in $line1 $line2 $line3 $line4 $line5; 
do
result=`grep $i $file`
if [ -z $result ]
then
cp $file $tmpfile
echo $i >> $tmpfile
else
cat $file | sed "s/$i/$newi/g" > $tmpfile
fi
done

我正在苦苦挣扎的是如何遍历 2 组变量并以格式添加缺失的字符串 $line1=$line1Arg

编辑 2

所以现在我有以下...

file=/folder/file
tmpfile=$file.tmp

#  strings to find and modify / add
line1=text string 1
line2=text string 2
line3=text string 3
line4=text string 4
line5=text string 5
# new values
line1Arg=120
line2Arg=370
line3Arg=100
line4Arg=999
line5Arg=1000

for i in $line1 $line2 $line3 $line4 $line5;
do
for x in $line1Arg $line2Arg $lineArg $line4Arg $line5Arg;
do

result=`grep $i $file`
oldarg=`grep $i $file |cut -d"=" -f2`

if [ -z $result ]
then
cp $file $tmpfile
echo $i=$x >> $tmpfile
echo "$i added with value $x"

else

if [ "$oldarg" = "$x" ]; then
echo "$i already set - no change"

else
sed "s/$i=.*/$i=${x}/g" $file > $tmpfile
echo "$i changed to value $x"
fi
fi
done

但它没有将所有缺失的 $lineX=$lineXArg 添加到文件中? 我错过了什么?

编辑 3

好的所以使用 sh -x 我得到以下输出

> root@d802:/ # sh -x /data/local/build.sh
> + file=/sdcard/documents/build.txt
> + tmpfile=/sdcard/documents/build.txt.tmp
> + line1=wifi.supplicant_scan_interval
> + line2=ro.sf.lcd_density
> + line3=ro.media.enc.jpeg.quality
> + line4=rixh.wallace
> + line5=ro.telephony.call_ring.delay
> + line1Arg=120
> + line2Arg=370
> + line3Arg=99999999
> + line4Arg=999
> + line5Arg=1000
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + result='wifi.supplicant_scan_interval=120'
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=120
> + '[' -z 'wifi.supplicant_scan_interval=120' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/wifi.supplicant_scan_interval=120/wifi.supplicant_scan_interval=120/g'
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + result='wifi.supplicant_scan_interval=120'
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=120
> + '[' -z 'wifi.supplicant_scan_interval=120' ']'
> + >/sdcard/documents/build.txt.tmp
> + cat /sdcard/documents/build.txt
> + sed 's/wifi.supplicant_scan_interval=120/wifi.supplicant_scan_interval=370/g'
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + result='wifi.supplicant_scan_interval=120'
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=120
> + '[' -z 'wifi.supplicant_scan_interval=120' ']'
> + >/sdcard/documents/build.txt.tmp
> + sed 's/wifi.supplicant_scan_interval=120/wifi.supplicant_scan_interval=999/g'
> + cat /sdcard/documents/build.txt
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + result='wifi.supplicant_scan_interval=120'
> + grep wifi.supplicant_scan_interval /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=120
> + '[' -z 'wifi.supplicant_scan_interval=120' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/wifi.supplicant_scan_interval=120/wifi.supplicant_scan_interval=1000/g'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + result='ro.sf.lcd_density=370'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=370
> + '[' -z 'ro.sf.lcd_density=370' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.sf.lcd_density=370/ro.sf.lcd_density=120/g'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + result='ro.sf.lcd_density=370'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=370
> + '[' -z 'ro.sf.lcd_density=370' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.sf.lcd_density=370/ro.sf.lcd_density=370/g'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + result='ro.sf.lcd_density=370'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=370
> + '[' -z 'ro.sf.lcd_density=370' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.sf.lcd_density=370/ro.sf.lcd_density=999/g'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + result='ro.sf.lcd_density=370'
> + grep ro.sf.lcd_density /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=370
> + '[' -z 'ro.sf.lcd_density=370' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.sf.lcd_density=370/ro.sf.lcd_density=1000/g'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + result='ro.media.enc.jpeg.quality=100'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=100
> + '[' -z 'ro.media.enc.jpeg.quality=100' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.media.enc.jpeg.quality=100/ro.media.enc.jpeg.quality=120/g'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + result='ro.media.enc.jpeg.quality=100'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=100
> + '[' -z 'ro.media.enc.jpeg.quality=100' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.media.enc.jpeg.quality=100/ro.media.enc.jpeg.quality=370/g'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + result='ro.media.enc.jpeg.quality=100'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=100
> + '[' -z 'ro.media.enc.jpeg.quality=100' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.media.enc.jpeg.quality=100/ro.media.enc.jpeg.quality=999/g'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + result='ro.media.enc.jpeg.quality=100'
> + grep ro.media.enc.jpeg.quality /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=100
> + '[' -z 'ro.media.enc.jpeg.quality=100' ']'
> + cat /sdcard/documents/build.txt
> + >/sdcard/documents/build.txt.tmp
> + sed 's/ro.media.enc.jpeg.quality=100/ro.media.enc.jpeg.quality=1000/g'
> + grep rixh.wallace /sdcard/documents/build.txt
> + result=
> + grep rixh.wallace /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'rixh.wallace=120'
> + grep rixh.wallace /sdcard/documents/build.txt
> + result=
> + grep rixh.wallace /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'rixh.wallace=370'
> + grep rixh.wallace /sdcard/documents/build.txt
> + result=
> + grep rixh.wallace /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'rixh.wallace=999'
> + grep rixh.wallace /sdcard/documents/build.txt
> + result=
> + grep rixh.wallace /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'rixh.wallace=1000'
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + result=
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'ro.telephony.call_ring.delay=120'
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + result=
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'ro.telephony.call_ring.delay=370'
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + result=
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'ro.telephony.call_ring.delay=999'
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + result=
> + grep ro.telephony.call_ring.delay /sdcard/documents/build.txt
> + cut '-d=' -f2
> + oldarg=
> + '[' -z ']'
> + cp /sdcard/documents/build.txt /sdcard/documents/build.txt.tmp
> + >>/sdcard/documents/build.txt.tmp
> + echo 'ro.telephony.call_ring.delay=1000'

编辑 4

当前代码:

#/system/bin/sh

file=/sdcard/documents/build.txt
tmpfile=$file.tmp


# props to change

line1=wifi.supplicant_scan_interval

line2=ro.sf.lcd_density

line3=ro.media.enc.jpeg.quality

line4=rixh.wallace

line5=ro.telephony.call_ring.delay


# new values

line1Arg=120

line2Arg=480

line3Arg=99999999

line4Arg=999

 line5Arg=6666666666666



for i in $line1 $line2 $line3 $line4 $line5; do

for x in $line1Arg $line2Arg $lineArg $line4Arg $line5Arg; do

result=`grep $i $file`
oldarg=`grep $i $file |cut -d"=" -f2`

if [ -z $result ]
then
cp $file $tmpfile
echo $i=$x >> $tmpfile;
echo "$i added with value $x"
else
cat $file | sed "s/$i=$oldarg/$i=$x/g"> $tmpfile
echo "$i edited to $x"

fi

done
done

和输出:

ro.telephony.call_ring.delay added with value 6666666666666
wifi.supplicant_scan_interval edited to 120
wifi.supplicant_scan_interval edited to 480
wifi.supplicant_scan_interval edited to 999
wifi.supplicant_scan_interval edited to 6666666666666
ro.sf.lcd_density edited to 120
ro.sf.lcd_density edited to 480
ro.sf.lcd_density edited to 999
ro.sf.lcd_density edited to 6666666666666
ro.media.enc.jpeg.quality edited to 120
ro.media.enc.jpeg.quality edited to 480
ro.media.enc.jpeg.quality edited to 999
ro.media.enc.jpeg.quality edited to 6666666666666
rixh.wallace added with value 120
rixh.wallace added with value 480
rixh.wallace added with value 999
rixh.wallace added with value 6666666666666
ro.telephony.call_ring.delay added with value 120
ro.telephony.call_ring.delay added with value 480
ro.telephony.call_ring.delay added with value 999
 ro.telephony.call_ring.delay added with value 6666666666666

让我们试试这个

#/system/bin/sh

file=/sdcard/documents/build.txt
tmpfile=$file.tmp


# props to change

line1=wifi.supplicant_scan_interval
line2=ro.sf.lcd_density
line3=ro.media.enc.jpeg.quality
line4=rixh.wallace
line5=ro.telephony.call_ring.delay


# new values

line1Arg=120
line2Arg=480
line3Arg=99999999
line4Arg=999
line5Arg=6666666666666

linex=$line1,$line2,$line3,$line4,$line5    

for x in $line1Arg $line2Arg $lineArg $line4Arg $line5Arg; do

#might need to be -f0, I forget syntax
i=`echo $linex | cut -d"," -f1`

#this is from some code I found online, not sure if it's right.
linex=${linex#*,}

result=`grep $i $file`
oldarg=`grep $i $file |cut -d"=" -f2`

if [ -z $result ]
then
cp $file $tmpfile
echo $i=$x >> $tmpfile;
echo "$i added with value $x"
else
cat $file | sed "s/$i=$oldarg/$i=$x/g"> $tmpfile
echo "$i edited to $x"

fi

done
done

旧东西

首先 grep 查看文件(或字符串或其他)是否包含您的值

 result=`grep $i $file`
 if [ -z $result ] 
 then
 cp $file $newfile
 echo "$i" >> $newfile
 else
 cat $file | sed "s/$i/$newi/g" > $newfile
 fi

在这种情况下,sed 命令包含用于替换的 "s"、您的旧字符串、您的新字符串,以及用于全局执行的 "g"(而不是每个 file/line/etc 一次) .如果它不包含在文件中,我还修改了它添加您的值的方式。 -z 检查字符串的长度是否为零(如果 grep 没有返回任何内容,则为零)并 >> 附加到文件的末尾。

编辑:

另外,关闭评论,我觉得我应该解释一下“|” , ">" 和 ">>"

“|”管道,管道输出到另一个函数。也就是说,它使用管道左侧命令的 stdOut 的输出作为管道右侧命令的 stdIn 的输入。如果未指定输入文件,许多 shell 命令使用 stdIn。这对于将命令链接在一起非常有效。

">" 重定向,将输出重定向到文件而不是 stdOut。如果(就像您的情况一样)您想要将输出移动到文件,而不仅仅是将其放在控制台中,这将很有用。不过,这将从头开始写入文件,有效地覆盖重定向到的任何文件输出。

">>" append,将信息追加到文件末尾,而不是在 stdOut 中显示。这意味着该文件可以持久存在,而且还向其中添加了程序的输出。如果您需要在文件末尾添加更多内容,这将很有用。

可能很难看出它们在哪里有用,但我希望它至少能阐明它们的工作原理以及我为什么选择在回答您的问题时使用它们。

编辑2: 将 "then" 添加到我认为是如何发出结构化 "if" 语句(例如 if-else)而不是简单的 if(只是如果,没有其他)的代码。我的 shell 编程有点生疏,希望对您有所帮助。

编辑3: 将 end-if 更改为 fi(因为这是 shell 中的正确语法。还将 $i 更改为 Echo $i,因为重定向指示命令的输出。

如果你需要在其中放置一个循环,其中 $i 是 $linexarg 中要匹配的每个变量,它看起来像这样

for i in $linexarg
do
#above code
done 

Here 是我用来确定这一点的参考。

编码愉快!如果您有任何问题,请发表评论。

sed 's/text string/value/' file > file.new

sed is a "stream editor" -- you pass it files or even pipe text into it, and it can manipulate the text in a Turing number of ways. This program reads the file (line-by-line), uses the s/// 命令搜索正则表达式 "text string",并将每行的第一个实例替换为文本 "value".