Shell 脚本读取输入并复制和粘贴

发布于 2024-10-13 07:07:58 字数 797 浏览 1 评论 0原文

好吧,我想这样做,我正在尝试编写一个脚本,使用此代码询问用户他们想要什么,我可以记录他们的输入。

#!/bin/bash

read inputline
what="$inputline"
echo $what

然后,我有另一个文件放置在该路径 /system/etc/99oc 的其他位置,如下所示

Some text some more text (want user input Pasted Here) more text more text

所以脚本需要能够接受输入并将其粘贴到另一个文档的文本之间。这可能吗?替代品?

感谢您的帮助

编辑 是的,抱歉,这是第二个文件中的每一行,这些行将被复制到

echo "1 '输入在此处'" > /proc/overclock/mpu_opps

echo "2 '输入在此处'" > /proc/overclock/mpu_opps

echo "3 '输入在此处'" > /proc/overclock/mpu_opps

echo "4 '输入在此处'" > /proc/overclock/mpu_opps

echo "5 '输入在此处'" > /proc/overclock/mpu_opps

echo "6 '输入在此处'" > /proc/overclock/mpu_opps

其中“输入在此处”而没有“ ”是应该粘贴输入的位置

也刚刚了解到我可以执行 cp $what

感谢您的帮助

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 $what

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执妄 2024-10-20 07:07:58

尝试一下:

#!/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

sed 命令将对文件进行适当的更改(对于 GNU sed)。如果您运行的是 OS X,则需要备份扩展。如果您运行的 sed 版本没有 -i,则需要将输出重定向到临时文件并重命名。

请务必在测试文件上使用它,以确保它符合您的要求。

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.

如此安好 2024-10-20 07:07:58

是的,这可以通过 sedawk 之类的东西来实现,但我们需要一些东西来了解(希望将用户输入粘贴到此处) 应该去。即,它是否总是插入到以“Some”开头的行中的第 6 个和第 7 个单词之间,等等。

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.

浮生面具三千个 2024-10-20 07:07:58

使用 sed 可以轻松完成。

无论使用什么操作(插入、添加或交换),语法都将保持不变:

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

检查 http://en.kioskea.net/faq/1454-inserting-text-in-a-file 获取更多帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文