我正在尝试编写修改文件的脚本。 我想检查文件中的已定义文本,如果它存在则更改它,如果它没有添加它。 要更改的文本在变量中定义:
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
答案 0 :(得分:0)
我们试试这个
#/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
OLD STUFF
首先看看文件(或字符串或其他)是否包含您的值
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”以全局执行(而不是每个文件/行/等一次)。如果它没有包含在文件中,我还修改了它添加值的方式。 -z检查一个字符串的长度是否为零(如果grep没有返回任何内容,则为>)>>追加到文件的末尾。
编辑:
另外,关闭评论,我觉得我应该解释“|” ,“>”和“>>”
“|”管道,管道输出到另一个功能。也就是说,它使用输出到管道左侧的命令的stdOut作为管道右侧命令的stdIn的输入。如果未指定输入文件,则许多shell命令使用stdIn。这对于将命令链接在一起非常有效。
“>” 中重定向,将输出重定向到文件而不是stdOut。如果(在您的情况下)您希望将输出移动到文件,而不是将其放在控制台中,这将非常有用。这将从头开始写入文件,有效地覆盖重定向到的文件输出。
“>>” 中追加,将信息附加到文件的末尾,而不是在stdOut中显示它。这意味着文件可以保留,但也添加了程序的输出。如果您需要在文件末尾添加更多内容,这非常有用。
可能很难看出有用的地方,但我希望它至少可以说明它们的工作原理以及我在回答你的问题时选择使用它们的原因。
EDIT2: 将“then”添加到代码中,我认为这是如何发出结构化的“if”语句(例如if-else),而不仅仅是一个简单的if(只是if,no else)。我的shell编程有点生疏,我希望这会有所帮助。
EDIT3: 将end-if更改为fi(因为这是shell中的正确语法。同时将$ i更改为Echo $ i,因为重定向指示命令的输出。
如果你需要在这里放一个循环,其中$ i是$ linexarg中匹配的每个变量,它看起来像这样
for i in $linexarg
do
#above code
done
Here是我用来确定的参考。
快乐的编码!如果您有任何问题,请发表评论。
答案 1 :(得分:0)