Skip to main content
deleted 252 characters in body
Source Link
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 237

I'm working on a script and I need to build the tar command dynamically. I've been spending hours on this without being able to make it work... Bash and its string management is totally f'd up...

I've tried a looooooottttt of things, hereHere are two examples to illustrate what I'm trying to do :

#!/bin/bash

TAR_ME="/tmp"

EXCLUDE=("/tmp/hello hello" "/tmp/systemd*" "/tmp/Temp*")
_tar="tar "`printf -- '--exclude="%s" ' "${EXCLUDE[@]}"`" -zcf tmp.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

echo -e "\n\nNEXT:\n\n"

EXCLUDE=("--exclude=/tmp/hello\ hello" "--exclude=/tmp/systemd*" "--exclude=/tmp/Temp*")
_tar="tar "`printf -- '%s ' "${EXCLUDE[@]}"`" -zcf test.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

I want to be able to use _tar as a command, I've been able to make it work with classic path, but I need it to work with spaces in folders' name... And every single time I got errors that look like :

COMMAND: tar --exclude="/tmp/hello hello" --exclude="/tmp/systemd*" --exclude="/tmp/Temp*"  -zcf tmp.tar.gz /tmp
tar: hello": Cannot stat: No such file or directory

COMMAND: tar --exclude=/tmp/hello\ hello --exclude=/tmp/systemd* --exclude=/tmp/Temp*  -zcf test.tar.gz 
tar: hello: Cannot stat: No such file or directory

Just one thing you need to know, I need my script to work on very old machines, meaning I can't use last bash features... I hope a bash genious will see my post because I've really been struggling on this one :(

I'm working on a script and I need to build the tar command dynamically. I've been spending hours on this without being able to make it work... Bash and its string management is totally f'd up...

I've tried a looooooottttt of things, here are two examples to illustrate what I'm trying to do :

#!/bin/bash

TAR_ME="/tmp"

EXCLUDE=("/tmp/hello hello" "/tmp/systemd*" "/tmp/Temp*")
_tar="tar "`printf -- '--exclude="%s" ' "${EXCLUDE[@]}"`" -zcf tmp.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

echo -e "\n\nNEXT:\n\n"

EXCLUDE=("--exclude=/tmp/hello\ hello" "--exclude=/tmp/systemd*" "--exclude=/tmp/Temp*")
_tar="tar "`printf -- '%s ' "${EXCLUDE[@]}"`" -zcf test.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

I want to be able to use _tar as a command, I've been able to make it work with classic path, but I need it to work with spaces in folders' name... And every single time I got errors that look like :

COMMAND: tar --exclude="/tmp/hello hello" --exclude="/tmp/systemd*" --exclude="/tmp/Temp*"  -zcf tmp.tar.gz /tmp
tar: hello": Cannot stat: No such file or directory

COMMAND: tar --exclude=/tmp/hello\ hello --exclude=/tmp/systemd* --exclude=/tmp/Temp*  -zcf test.tar.gz 
tar: hello: Cannot stat: No such file or directory

Just one thing you need to know, I need my script to work on very old machines, meaning I can't use last bash features... I hope a bash genious will see my post because I've really been struggling on this one :(

I'm working on a script and I need to build the tar command dynamically.

Here are two examples to illustrate what I'm trying to do :

#!/bin/bash

TAR_ME="/tmp"

EXCLUDE=("/tmp/hello hello" "/tmp/systemd*" "/tmp/Temp*")
_tar="tar "`printf -- '--exclude="%s" ' "${EXCLUDE[@]}"`" -zcf tmp.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

echo -e "\n\nNEXT:\n\n"

EXCLUDE=("--exclude=/tmp/hello\ hello" "--exclude=/tmp/systemd*" "--exclude=/tmp/Temp*")
_tar="tar "`printf -- '%s ' "${EXCLUDE[@]}"`" -zcf test.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

I want to be able to use _tar as a command, I've been able to make it work with classic path, but I need it to work with spaces in folders' name. And every single time I got errors that look like :

COMMAND: tar --exclude="/tmp/hello hello" --exclude="/tmp/systemd*" --exclude="/tmp/Temp*"  -zcf tmp.tar.gz /tmp
tar: hello": Cannot stat: No such file or directory

COMMAND: tar --exclude=/tmp/hello\ hello --exclude=/tmp/systemd* --exclude=/tmp/Temp*  -zcf test.tar.gz 
tar: hello: Cannot stat: No such file or directory

Just one thing you need to know, I need my script to work on very old machines, meaning I can't use last bash features.

Tweeted twitter.com/StackUnix/status/1047909368208023556
added 21 characters in body
Source Link
ShellCode
  • 235
  • 1
  • 2
  • 10

I'm working on a script and I need to build the tar command dynamically. I've been spending hours on this without being able to make it work... Bash and its string management is totally f'd up...

I've tried a looooooottttt of things, here are two examples to illustrate what I'm trying to do :

#!/bin/bash

TAR_ME="/tmp"

EXCLUDE=("/tmp/hello hello" "/tmp/systemd*" "/tmp/Temp*")
_tar="tar "`printf -- '--exclude="%s" ' "${EXCLUDE[@]}"`" -zcf tmp.tar.gz /tmp"gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

echo -e "\n\nNEXT:\n\n"

EXCLUDE=("--exclude=/tmp/hello\ hello" "--exclude=/tmp/systemd*" "--exclude=/tmp/Temp*")
_tar="tar "`printf -- '%s ' "${EXCLUDE[@]}"`" -zcf test.tar.gz "gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

I want to be able to use _tar as a command, I've been able to make it work with classic path, but I need it to work with spaces in folders' name... And every single time I got errors that look like :

COMMAND: tar --exclude="/tmp/hello hello" --exclude="/tmp/systemd*" --exclude="/tmp/Temp*"  -zcf tmp.tar.gz /tmp
tar: hello": Cannot stat: No such file or directory

COMMAND: tar --exclude=/tmp/hello\ hello --exclude=/tmp/systemd* --exclude=/tmp/Temp*  -zcf test.tar.gz 
tar: hello: Cannot stat: No such file or directory

Just one thing you need to know, I need my script to work on very old machines, meaning I can't use last bash features... I hope a bash genious will see my post because I've really been struggling on this one :(

I'm working on a script and I need to build the tar command dynamically. I've been spending hours on this without being able to make it work... Bash and its string management is totally f'd up...

I've tried a looooooottttt of things, here are two examples to illustrate what I'm trying to do :

#!/bin/bash

EXCLUDE=("/tmp/hello hello" "/tmp/systemd*" "/tmp/Temp*")
_tar="tar "`printf -- '--exclude="%s" ' "${EXCLUDE[@]}"`" -zcf tmp.tar.gz /tmp"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

echo -e "\n\nNEXT:\n\n"

EXCLUDE=("--exclude=/tmp/hello\ hello" "--exclude=/tmp/systemd*" "--exclude=/tmp/Temp*")
_tar="tar "`printf -- '%s ' "${EXCLUDE[@]}"`" -zcf test.tar.gz "
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

I want to be able to use _tar as a command, I've been able to make it work with classic path, but I need it to work with spaces in folders' name... And every single time I got errors that look like :

COMMAND: tar --exclude="/tmp/hello hello" --exclude="/tmp/systemd*" --exclude="/tmp/Temp*"  -zcf tmp.tar.gz /tmp
tar: hello": Cannot stat: No such file or directory

COMMAND: tar --exclude=/tmp/hello\ hello --exclude=/tmp/systemd* --exclude=/tmp/Temp*  -zcf test.tar.gz 
tar: hello: Cannot stat: No such file or directory

Just one thing you need to know, I need my script to work on very old machines, meaning I can't use last bash features... I hope a bash genious will see my post because I've really been struggling on this one :(

I'm working on a script and I need to build the tar command dynamically. I've been spending hours on this without being able to make it work... Bash and its string management is totally f'd up...

I've tried a looooooottttt of things, here are two examples to illustrate what I'm trying to do :

#!/bin/bash

TAR_ME="/tmp"

EXCLUDE=("/tmp/hello hello" "/tmp/systemd*" "/tmp/Temp*")
_tar="tar "`printf -- '--exclude="%s" ' "${EXCLUDE[@]}"`" -zcf tmp.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

echo -e "\n\nNEXT:\n\n"

EXCLUDE=("--exclude=/tmp/hello\ hello" "--exclude=/tmp/systemd*" "--exclude=/tmp/Temp*")
_tar="tar "`printf -- '%s ' "${EXCLUDE[@]}"`" -zcf test.tar.gz"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

I want to be able to use _tar as a command, I've been able to make it work with classic path, but I need it to work with spaces in folders' name... And every single time I got errors that look like :

COMMAND: tar --exclude="/tmp/hello hello" --exclude="/tmp/systemd*" --exclude="/tmp/Temp*"  -zcf tmp.tar.gz /tmp
tar: hello": Cannot stat: No such file or directory

COMMAND: tar --exclude=/tmp/hello\ hello --exclude=/tmp/systemd* --exclude=/tmp/Temp*  -zcf test.tar.gz 
tar: hello: Cannot stat: No such file or directory

Just one thing you need to know, I need my script to work on very old machines, meaning I can't use last bash features... I hope a bash genious will see my post because I've really been struggling on this one :(

Source Link
ShellCode
  • 235
  • 1
  • 2
  • 10

Build a command dynamically

I'm working on a script and I need to build the tar command dynamically. I've been spending hours on this without being able to make it work... Bash and its string management is totally f'd up...

I've tried a looooooottttt of things, here are two examples to illustrate what I'm trying to do :

#!/bin/bash

EXCLUDE=("/tmp/hello hello" "/tmp/systemd*" "/tmp/Temp*")
_tar="tar "`printf -- '--exclude="%s" ' "${EXCLUDE[@]}"`" -zcf tmp.tar.gz /tmp"
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

echo -e "\n\nNEXT:\n\n"

EXCLUDE=("--exclude=/tmp/hello\ hello" "--exclude=/tmp/systemd*" "--exclude=/tmp/Temp*")
_tar="tar "`printf -- '%s ' "${EXCLUDE[@]}"`" -zcf test.tar.gz "
echo COMMAND: "${_tar}"
${_tar} "$TAR_ME"

I want to be able to use _tar as a command, I've been able to make it work with classic path, but I need it to work with spaces in folders' name... And every single time I got errors that look like :

COMMAND: tar --exclude="/tmp/hello hello" --exclude="/tmp/systemd*" --exclude="/tmp/Temp*"  -zcf tmp.tar.gz /tmp
tar: hello": Cannot stat: No such file or directory

COMMAND: tar --exclude=/tmp/hello\ hello --exclude=/tmp/systemd* --exclude=/tmp/Temp*  -zcf test.tar.gz 
tar: hello: Cannot stat: No such file or directory

Just one thing you need to know, I need my script to work on very old machines, meaning I can't use last bash features... I hope a bash genious will see my post because I've really been struggling on this one :(