sh文件运行问题

发布于 2024-10-29 15:13:32 字数 504 浏览 6 评论 0原文

我编写为 sh 文件。当我执行它时,它无法正常工作。但是通过使用相同的命令独立地命令提示符,它可以工作。可能是什么问题?我是 shell 脚本编写的新手。所以任何帮助将不胜感激。这是我的sh文件,其中包括:

mkdir prelab5
cd prelab5
wget http://cse.yeditepe.edu.tr/~oturkes/spring2011/cse232/week7/rms.txt &
grep "free" rms.txt | tail -n 10 > free.txt
cd ..
find /home/misafir -perm -755
chmod 744 file.txt
grep "free" rms.txt | wc
tr 'a-z' 'A-Z'<free.txt

后台进程意味着它在后台工作,让我们同时使用终端。此外,当我们让该进程在后台工作时,我们可以看到该后台的唯一进程ID(pid)因此,通过使用这个唯一的pid,我们可以在这个后台工作进程上执行一些任何命令。

I wrote as sh file.When I execute it it doesn't work correctly.But by using same commands to command prompt independently it works.What may be the problem?I am a newbie in shell scripting.So any help will be appreciated.Here is my sh file including:

mkdir prelab5
cd prelab5
wget http://cse.yeditepe.edu.tr/~oturkes/spring2011/cse232/week7/rms.txt &
grep "free" rms.txt | tail -n 10 > free.txt
cd ..
find /home/misafir -perm -755
chmod 744 file.txt
grep "free" rms.txt | wc
tr 'a-z' 'A-Z'<free.txt

Background process means that it is worked in the background and let us to use terminal simultaneously.Also,while we let working the process in the background,we can see a unique process id (pid) of this background process on the screen.Therefore,by using this unique pid we can execute some any commands on this bckground working process.

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

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

发布评论

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

评论(4

煮酒 2024-11-05 15:13:32

删除 wget 命令末尾的 & 符号。

shell 执行命令的速度比您键入命令的速度快;当你 grep 时,“免费”wget 可能还没有完成

remove the ampersand at the end of wget command.

shell executes the commands faster than you type them; when you grep the "free" wget probably isnt finished yet

你怎么敢 2024-11-05 15:13:32

在Grep运行时,WGET还没有完成(甚至可能还没有启动)。删除WGE线末端的and和and和,然后将其添加到脚本调用中。

wget hasn't finished (and probably hasn't even started) by the time grep runs. Remove the ampersand at the end of the wget line, and add it to the script invocation instead.

痕至 2024-11-05 15:13:32

第一个问题是 &在 wget 线上。它在后台启动 wget,然后立即 grep rms.txt - 但该文件可能尚未完成下载。删除 &应该没问题。

第二个问题是,在您 cd .. 后,rms.txt 和 free.txt 不再位于当前工作目录中,因此最后两行引用的文件似乎不存在。

最后,目前还不清楚你想通过 find 来完成什么 - 正如你所写的那样,它只会将一堆内容打印到终端。同样,也不清楚 file.txt 应该是什么 - 它没有在其他任何地方被引用。

一般来说,如果您除了问题代码之外还指出您的目标是什么,将会很有帮助。

The first problem is the & on the wget line. It starts the wget in the background, and then immediately greps rms.txt - but that file probably hasn't finished being downloaded yet. Remove the & and it should be fine.

The second problem is that after you cd .., rms.txt and free.txt are no longer in the current working directory, so the last two lines refer to files that appear not to exist.

Lastly, it's very unclear what you're trying to accomplish with the find - as you've written it, it'll just print a bunch of stuff to the terminal. Similarly, it's unclear what file.txt is supposed to be - it's not referred to anywhere else.

In general, it's helpful if you give an indication of what your goal is in addition to the problem code.

情徒 2024-11-05 15:13:32

您的脚本中存在不同的问题。目前我不确定您是否知道每一行在做什么。所以

mkdir prelab5 # create a dire prelab5
cd prelab5  # change dir to prelab5
wget http://cse.yeditepe.edu.tr/.../rms.txt & #dl rms.txt
grep "free" rms.txt | tail -n 10 > free.txt

最后一行脚本将 10 行包含 free 的 rms.txt 放入 free.txt 中。此时 rms.txt 可能还不存在。你最好删除 &在上一行的末尾,以便顺序执行每一行。

cd .. # go back to the parent directory
find /home/misafir -perm -755

该行在其他地方搜索文件,但结果未被使用。

chmod 744 file.txt  # Modify a file we didn't hear about before 
grep "free" rms.txt | wc # Should fail since rms.txt is not in this directory
tr 'a-z' 'A-Z'<free.txt # Doesn't work this way

There are different problems in your script. At the point that I am not sure that you know what each line is doing. so

mkdir prelab5 # create a dire prelab5
cd prelab5  # change dir to prelab5
wget http://cse.yeditepe.edu.tr/.../rms.txt & #dl rms.txt
grep "free" rms.txt | tail -n 10 > free.txt

This last script line puts 10 lines of rms.txt containing free in free.txt. At this point rms.txt probably does not exist yet. You better remove the & at the end of the previous line in order to execute each line sequencially.

cd .. # go back to the parent directory
find /home/misafir -perm -755

This line search for files somewhere else but the result is not used.

chmod 744 file.txt  # Modify a file we didn't hear about before 
grep "free" rms.txt | wc # Should fail since rms.txt is not in this directory
tr 'a-z' 'A-Z'<free.txt # Doesn't work this way
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文