Bash script to automate wget tar cd using word pointers and word modifiers

How can I automate the following using a bash script using word pointers 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 below, I get errors because word pointers and word modifiers do not work the same in bash scripts, as they do in the 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
+3
source share
2 answers

Replacing history works on the command line. In a script, you can use the parameter extension.

#!/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"
+4
source

, , , :

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

bash script , script:

version=$1

.., .

+1

All Articles