bash脚本使用word指示符和单词修饰符自动化wget tar cd(bash script to automate wget tar cd using word designators and w

编程入门 行业动态 更新时间:2024-10-25 16:23:47
bash脚本使用word指示符和单词修饰符自动化wget tar cd(bash script to automate wget tar cd using word designators and word modifiers)

如何使用单词指示符和单词修饰符或类似的东西使用bash shell脚本自动执行以下操作?

root@server:/tmp# wget -q http://download.zeromq.org/zeromq-2.2.0.tar.gz root@server:/tmp# tar -xzf !$:t tar -xzf zeromq-2.2.0.tar.gz root@server:/tmp# cd !$:r:r cd zeromq-2.2.0 root@server:/tmp/zeromq-2.2.0#

当我尝试类似下面的内容时,我会收到错误,因为单词指示符和单词修饰符在bash脚本中的效果与在shell中的表现方式不同:

Bash Shell脚本示例1:

#!/usr/bin/env bash wget -q http://download.zeromq.org/zeromq-2.2.0.tar.gz && tar -xzf !$:t && cd !$:r:r root@server:/tmp# ./install.sh tar (child): Cannot connect to !$: resolve failed gzip: stdin: unexpected end of file tar: Child returned status 128 tar: Error is not recoverable: exiting now

Bash Shell脚本示例2:

#!/usr/bin/env bash wget -q http://download.zeromq.org/zeromq-2.2.0.tar.gz tar -xzf !$:t cd !$:r:r root@server:/tmp# ./install.sh tar (child): Cannot connect to !$: resolve failed gzip: stdin: unexpected end of file tar: Child returned status 128 tar: Error is not recoverable: exiting now ./install.sh: line 11: cd: !$:r:r: No such file or directory

How can I automate the following with a bash shell script using word designators and word modifiers or something similar?

root@server:/tmp# wget -q http://download.zeromq.org/zeromq-2.2.0.tar.gz root@server:/tmp# tar -xzf !$:t tar -xzf zeromq-2.2.0.tar.gz root@server:/tmp# cd !$:r:r cd zeromq-2.2.0 root@server:/tmp/zeromq-2.2.0#

When I try something like the below I get errors because word designators and word modifiers don't appear to work the same way in bash scripts as they do in a shell:

Bash Shell Script Example 1:

#!/usr/bin/env bash wget -q http://download.zeromq.org/zeromq-2.2.0.tar.gz && tar -xzf !$:t && cd !$:r:r root@server:/tmp# ./install.sh tar (child): Cannot connect to !$: resolve failed gzip: stdin: unexpected end of file tar: Child returned status 128 tar: Error is not recoverable: exiting now

Bash Shell Script Example 2:

#!/usr/bin/env bash wget -q http://download.zeromq.org/zeromq-2.2.0.tar.gz tar -xzf !$:t cd !$:r:r root@server:/tmp# ./install.sh tar (child): Cannot connect to !$: resolve failed gzip: stdin: unexpected end of file tar: Child returned status 128 tar: Error is not recoverable: exiting now ./install.sh: line 11: cd: !$:r:r: No such file or directory

最满意答案

历史替换在命令行中起作用。 在脚本中,您可以使用参数扩展。

#!/usr/bin/env bash url=http://download.zeromq.org/zeromq-2.2.0.tar.gz wget -q "$url" tarfile=${url##*/} # strip off the part before the last slash tar -xzf "$tarfile" dir=${tarfile%.tar.gz} # strip off ".tar.gz" cd "$dir"

History substitution works at the command line. In a script, you can use parameter expansion.

#!/usr/bin/env bash url=http://download.zeromq.org/zeromq-2.2.0.tar.gz wget -q "$url" tarfile=${url##*/} # strip off the part before the last slash tar -xzf "$tarfile" dir=${tarfile%.tar.gz} # strip off ".tar.gz" cd "$dir"

更多推荐

本文发布于:2023-08-01 13:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   单词   指示   修饰符   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!