bash script to automate wget tar cd using word designators and word modifiers - bash

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

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"

If the example provided is the only issue you are trying to resolve perhaps the following can help:
version="2.2.0"
wget -q http://download.zeromq.org/zeromq-${version}.tar.gz
tar -xzf zeromq-${version}.tar.gz
cd zeromq-${version}
In a bash script version could be the first option passed to the script:
version=$1
This does not contain error handling and so on but should get you started.

Related

must use full path with `-prune` in the find command? [duplicate]

This a really short question. But is there something syntatically wrong with placing a variable $example as an argument for tar in a bash file?
I have the file written as
//only portion that really matters
#!/bin/bash
...
tar -cvpzf $filename $backup_source
//here's the actual code
#!/bin/bash
backup_source="~/momobobo"
backup_dest="~/momobobo_backup/"
dater=`date '+%m-%d-%Y-%H-%M-%S'`
filename="$backup_dest$dater.tgz"
echo “Backing Up your Linux System”
tar -cvpzf $filename $backup_source
echo tar -cvpzf $filename $backup_source
echo “Backup finished”
//and heres the error
“Backing Up your Linux System”
tar: ~/momobobo: Cannot stat: No such file or directory
tar (child): ~/momobobo_backup/07-02-2013-18-34-12.tgz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar -cvpzf ~/momobobo_backup/07-02-2013-18-34-12.tgz ~/momobobo
Notice the "echo tar ...". When I copy and paste the output and run it in my terminal there is no problem taring the file. I'm currently running Xubuntu and I already did an update.
~ doesn't expand to your home directory in double quotes.
Just remove the double quotes:
backup_source=~/momobobo
backup_dest=~/momobobo_backup/
In cases where you have things you would want to quote, you can use ~/"momobobo"

Extracting file with folders then using bash completion

I don't work much with bash scripting and was trying to do something like this:
#!/bin/bash
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-i686-static.tar.xz
tar xvf ffmpeg-git-i686-static.tar.xz
cd ./ffmpeg-git-20210501-i686-static/
cp ./ffmpeg-git-20210501-i686-static/ffmpeg /etc/bin
Is there a variable or a way I can determine what that extracted folder is called during script execution. For example with bash completion at the command line I would use cd ./ffmpeg (since I know it starts with ffmpeg)
Make sense?
There is no way to know beforehand the directory structure, but you can use a wildcard in the copy command:
cp ./*/ffmpeg /etc/bin
Although, I do not recommend installing tarball extracted executable within /etc/bin.
I'd put a symbolic link inside /usr/local/bin/ instead:
#!/usr/bin/env sh
__opwd="$PWD"
trap 'cd "$__opwd"' EXIT ABRT INT
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-i686-static.tar.xz || exit 1
# Create place where to install the unpacked archive
mkdir -p '/opt/ffmpeg-git-i686-static' || exit 1
# Unpack the archive
cd '/opt/ffmpeg-git-i686-static' || exit 1
tar xf "$__opwd/ffmpeg-git-i686-static.tar.xz" || exit 1
# Create a symbolic link from the ffmpeg command into `/usr/local/bin/`
ln -sf /opt/ffmpeg-git-i686-static/*/ffmpeg /usr/local/bin/

Short tar script: 'command not found' when trying to add today's date to a compressed file name

I'm trying to create a script that will do the following:
create /home/testuser/backup as a directory if it doesn't exist (and won't show an error message if it does exist)
obtain the current date and store it as a variable
Using Tar:
backup the entire projectfiles directory
backup is compressed, in gzip format, in archive format
uses the stored variable to include the date in the tar filename
the backup goes to the /home/testuser/backup directory
create a log file called testuser.log with all messages generated by the tar command (using verbose mode)
save the log file in /home/testuser/backup/testuser.log
I'm having trouble with the command syntax and I don't quite understand what I'm doing wrong.
cd /home/testuser
mkdir -p /home/testuser/backup
today=$(date'+%d-%m-%y')
tar -zcvf testuserbackup-$today.tar.gz projectfiles &&
testuserbackup-$today.tar.gz /home/testuser/backup
testuserbackup-$today.tar.gz >> testuser.log 2>/dev/null
mv testuser.log /home/testuser/backup
When I try to run the script I get the following terminal output:
./script2.sh: line 6: date+%d-%m-%y: command not found
projectfiles/
projectfiles/budget/
projectfiles/budget/testuserbudget1.txt
projectfiles/budget/testuserbudget2.txt
projectfiles/old/
projectfiles/old/testuserold2.txt
projectfiles/old/testuserold1.txt
projectfiles/documents/
projectfiles/documents/testuserdoc2.txt
projectfiles/documents/testuserdoc1.txt
./script2.sh: line 7: testuserbackup-.tar.gz: command not found
I'm open to any suggestions. This task is from an old assignment from last semester that I'm revisiting for fun...
According to my old assignment notes this task should be able to be done in no more than 4 lines of code.
**EDIT:**Finished script (with assistance of John)
#!/bin/bash
mkdir -p /home/testuser/backup
today=$(date '+%d-%m-%y')
tar -zcvf backup/testuserbackup-"$today".tar.gz projectfiles >
backup/testuser.log 2>&1
You're missing a space:
today=$(date '+%d-%m-%y')
# ^
Additionally, these lines should all be combined:
tar -zcvf testuserbackup-$today.tar.gz projectfiles &&
testuserbackup-$today.tar.gz /home/testuser/backup
testuserbackup-$today.tar.gz >> testuser.log 2>/dev/null
mv testuser.log /home/testuser/backup
The log file needs to be created in the same line as the tar command, and making the tarball and the log file show up in the right location can be done by writing out their full paths. That gets rid of the need to move them later.
tar -zcvf backup/testuserbackup-"$today".tar.gz projectfiles > backup/testuser.log 2>&1
It's a good idea to capture stderr as well as stdout, so I changed 2>/dev/null to 2>&1.

How to pass a directory path containing spaces to the -C option of tar in a bash script?

i'm building up in a bash script a tar command like this:
/usr/bin/tar -cvjf /tmp/archive.tar.bz2 -X excl.txt -C '/cygdrive/c/Users/Utente/dir with spaces/' dir1 dir2 dir2
/usr/bin/tar: '/cygdrive/c/Users/Utente/dir: Cannot open: No such file or directory
if i test the command in the shell it works fine.
if i run the script the script complains. i think that something fails when tar tries to change directory.
of course i can change directory in the script an then avoid the use of the -C option but i miss the opportunity to use the -X option.

Error in executing command tar -xjf

While executing command in shell script like as follows i am getting an error please help
tar -xjf $tarfile
error is :
tar: option requires an argument -- f
Then the variable $tarfile is obiviously empty. Argument f requires a filename or a hyphen to denote stdin allowing for piping of data to tar. The j argument expects to find the tarfile compressed in bzip2 format.
In your script you would have to do something similar:
tarfile=/path/to/file.tar.bz2
tar -xjf $tarfile
or an example of piping:
ssh user#remotehost cat /path/to/file.tar.bz2|tar -xjf -
This happens because the $tarfile variable is empty.

Resources