Package Tar.Gz into a Shell Script

Package tar.gz into a shell script

There is a Linux Journal article explaining how to do this in detail, with code for packing the payload, etc. As Etan Reisner says in his comment, the extraction/installation script knows how to cut its tail off to obtain the payload which was concatenated earlier. Here's an example of how that works:

#!/bin/bash
# a self-extracting script header

# this can be any preferred output directory
mkdir ./output_dir

# determine the line number of this script where the payload begins
PAYLOAD_LINE=`awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' $0`

# use the tail command and the line number we just determined to skip
# past this leading script code and pipe the payload to tar
tail -n+$PAYLOAD_LINE $0 | tar xzv -C ./output_dir

# now we are free to run code in output_dir or do whatever we want

exit 0

# the 'exit 0' immediately above prevents this line from being executed
__PAYLOAD_BELOW__

Note the use of $0 to refer to the script itself.

To create the installer in the first place, you need to concatenate the code above and the tarball you want to install/deliver. If the script above is called extract.sh, and the payload is called payload.tar.gz, then this command would do the trick:

cat extract.sh payload.tar.gz > run_me.sh

How to untar all .tar.gz with shell-script?

for f in *.tar.gz
do
tar zxvf "$f" -C /path/tar
done

bash script code help to make zip/tar of several folders

# tar: (c)reate g(z)ip (v)erbose (f)ile [filename.tar.gz] [contents]...
tar -czvf /var/file/bkup.tar.gz /home/code/bots /var/config /var/system

# zip: (r)ecursive [filename.zip] [contents]...
zip -r /var/file/bkup.zip /home/code/bots /var/config /var/system

Setting Permissions in Shell Scripts Within .tar.gz files Using Azure DevOps

The task ArchiveFiles doesnot change the files permissions while archiving. I tested on my pipeline to archive a .sh file and extract the tar.gz on the linux system. The permissions of .sh file remained the same. So the issue you encountered above might because the origin boot.sh doesnot have the execute permission.

You can run the command ls -l main_folder/boot.sh to check the file's permission before achiving. And add the permission if it doesnot have execute permission.

- bash: |

ls -l main_folder/boot.sh
chmod u=rwx main_folder/boot.sh

- task: ArchiveFiles@2

installing a project by shell commands

Try this:

cd /cygdrive/d
tar xzvf DepOE-beta.tar.gz

cygwin exposes your windows drives under the /cygdrive directory. Note that you must use forward slashes "/" not backslashes to navigate directories in cygwin (as in linux/unix).

How to untar every type of tar file in a directory with bash script?

To get your script to work with minimal changes, use $afile whenever you want the variable's value. The dollar sign makes a variable reference; otherwise you just get the literal string 'afile'. Also get rid of the square brackets and instead echo the variable to grep.

for afile in `ls -1`; do
if echo "$afile" | grep '\.tar\.gz'
then
tar -xzf "$afile"
elif echo $afile | grep '\.tar\.xz'
then
tar -xJf "$afile"
elif echo "$afile" | grep '\.tar\.bz2'
then
tar -xjf "$afile"
else
echo "Something is wrong with the program"
fi
done

Since you're a bash beginner, let's look at various other ways you could write the script. I'd make a couple of improvements. For one, you shouldn't loop over ls. You can get the same thing by looping over *. Second, grep is a heavyweight tool. You can do some simple string comparisons with built-in shell constructs like [[ and ==.

for afile in *; do
if [[ "$afile" == *.tar.gz ]]; then
tar -xzf "$afile"
elif [[ "$afile" == *.tar.xz ]]; then
tar -xJf "$afile"
elif [[ "$afile" == *.tar.bz2 ]]; then
tar -xjf "$afile"
else
echo "Something is wrong with the program"
fi
done

Actually, this would be even nicer with a case statement. Let's try that. Also let's echo the error message to stderr with >&2. That's always a good idea.

for afile in *; do
case "$afile" in
*.tar.gz) tar -xzf "$afile";;
*.tar.xz) tar -xJf "$afile";;
*.tar.bz2) tar -xjf "$afile";;
*) echo "Something is wrong with the program" >&2
esac
done

We could even get rid of the error message if we just list the three types of files we want to loop over. Then there's no way to hit the else case.

for afile in *.tar.{gz,xz,bz2}; do
case "$afile" in
*.tar.gz) tar -xzf "$afile";;
*.tar.xz) tar -xJf "$afile";;
*.tar.bz2) tar -xjf "$afile";;
esac
done

Or a completely different way to do it: use find to find all the files and its -exec action to call a command for each file it finds. Here {} is a placeholder for the files it finds.

find . -name '*.tar.gz'  -exec tar -xzf {} \;
find . -name '*.tar.xz' -exec tar -xJf {} \;
find . -name '*.tar.bz2' -exec tar -xjf {} \;

Linux shell script to tar.gzip log files older than 1 month grouped by month

Here's the third solution for your updated question:

#!/usr/bin/env bash

LOGTYPES=$( ls *log* | sed -rn "s/([0-9]{6})[0-9]{2}.*$/\1/p" | sort -u )

# the sed command, item by item:
#
# s/ search and replace
# ([0-9]{6}) block of 6 digits, and store it
# [0-9]{2} followed by 2 more digits
# .*$ followed by any and all characters until the end of the input
# / replace all of that with
# \1 the first stored block (the 6 digits)
# /p print the output
#
# So this turns FailedAudit_20150101_000000.log into FailedAudit_201501

THIS_MONTH=$(date +%Y%m)
for LOG in $LOGTYPES; do
MONTH=${LOG: -6} # Last 6 characters of the LOGTYPE are YYYYMM

if [[ "$MONTH" -lt "$THIS_MONTH" ]]; then
LOG_FILES=$(ls ${LOG}*)
tar -czf ${LOG}.tar.gz ${LOG_FILES}
RC=$? # Check whether an error occured
if [[ "$RC" == "0" ]]; then
rm ${LOG_FILES}
fi
fi
done

Note: This assumes that the first block of 8 digits is the datestamp, and everything after that is not relevant for which archive it is to go to.

Update:
The sed script no longer outputs files that do not contain a timestamp.



Related Topics



Leave a reply



Submit