开发者

Shell Script read input and copy and paste

Okay i'm looking to do this i'm trying to write a script that will ask the user what they want using this code i can do that i records their input.

#!/bin/bash

read inputline
what="$inputline"
echo $wh开发者_运维百科at

I then have another file that is placed else where at this path /system/etc/99oc which looks like this

Some text some more text (want user input pasted here) more text more text

So the script needs to be able to take the input and paste it in between text in another document. Is this possible? alternatives?

Thank you for any help

Edit Yes sorry here are each lines in the second file that would be copied into

echo "1 'input goes here'" > /proc/overclock/mpu_opps

echo "2 'input goes here'" > /proc/overclock/mpu_opps

echo "3 'input goes here'" > /proc/overclock/mpu_opps

echo "4 'input goes here'" > /proc/overclock/mpu_opps

echo "5 'input goes here'" > /proc/overclock/mpu_opps

echo "6 'input goes here'" > /proc/overclock/mpu_opps

where 'input goes here' with out the ' ' is where the input should be pasted

also just learned i can do cp $what

Thanks for the help


Give this a try:

#!/bin/bash

read -r -p "Please enter some text" inputline
# attempt to sanitize the input by escaping some special characters
printf -v inputline '%q' "$inputline"
# insert the user input before the second quotation mark on each line in the file
sed -i "/^echo.*mpu_opps$/s/\"/$inputline\"/2 /system/etc/99oc

The sed command will make the change to the file in place (for GNU sed). If you're running OS X, a backup extension is required. If you're running a version of sed that doesn't have -i, you'll need to redirect the output to a temporary file and rename it.

Be sure and use this on a test file to make sure it does what you want.


Yes, this is possible with something like sed or awk but we would need something to key off of to know where (want user input pasted here) should go. I.e., will it always be inserted between the 6th and 7th word in the line that starts with 'Some', etc.


Can be easily done with sed.

Whatever operation (insertion, addition or exchange) is used, the syntax will remains the same:

sed '{/pattern/|/regexp/|n}{i|a|c}<text to be iserted>' file

Check http://en.kioskea.net/faq/1454-inserting-text-in-a-file for more help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜