English 中文(简体)
bash 脚本, 使用字词设计器和字改写器使 wgget tar cd 自动化
原标题:bash script to automate wget tar cd using word designators and word modifiers

我怎样才能使用字代和字改字或类似的东西, 将以下文字与shash 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 脚本 例 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
最佳回答

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

#!/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"
问题回答

如果所举的例子是你们试图解决的唯一问题,也许以下各点可以有所帮助:

version="2.2.0"
wget -q http://download.zeromq.org/zeromq-${version}.tar.gz
tar -xzf zeromq-${version}.tar.gz
cd zeromq-${version}

Bash 脚本版本中的第一个选项可以传给脚本 :

version=$1

此操作不包含错误处理等等, 但应该启动您 。





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签