Basic tar script fails to use variables

Newbie in linux, I'm trying to setup a simple backup script with tar as you can see below:

#!/bin/bash TIME=`date +%F_%H-%M` FILENAME=backup-$TIME.tar.gz SRCDIR="~/PI/tutos/* ~/scripts/*" DESTDIR=/media/sf_ubuntuSharedFolder/backups tar cvpzf $FILENAME $SRCDIR 

However, the usage of $SRCDIR causes a tar error that I do not have if I just expand it myself.

[email protected]:~$ save tar: ~/PI/tutos/*: Cannot stat: No such file or directory tar: ~/scripts/*: Cannot stat: No such file or directory 

What is wrong here at using a shell variable ? Otherwise, how can I pass a list of folders to tar in a single line ?

Thank you for any hints, Alex


The issue is that ~-expansion is not performed upon variable expansion (when you refer to that $SRCDIR unquoted) nor inside double quotes (when you assign that SRCDIR variable).

In leaving $SRCDIR unquoted, you're invoking the split+glob operator. That is the string that is stored in that scalar $SRCDIR variable (~/PI/tutos/* ~/scripts/*) is first split according to $IFS (blanks by default), and then each word undergoes globbing, that is are treated as patterns that expand to the list of matching files.

Because ~ is not expanded to your home directory there, that ~ is just treated like any other character, so it's just looking for files in the ~/PI/tutos directory, where ~ would be a directory in the current directory, which in your case doesn't exist.

Best here would be to make $SRCDIR an array and have the globs expanded at the time of the assignment:

SRCDIR=(~/PI/tutos/* ~/scripts/*) # ~ and globs expanded at this point
tar cvpzf "$FILENAME" "${SRCDIR[@]}"

Note that applying the split+glob operator on $FILENAME doesn't make sense, so we're disabling it by quoting $FILENAME.

Note that if ~/PI/tutos/* doesn't match, it will be left as-is, so you'd still get an error from tar. To avoid that, you could do:

shopt -s nullglob # remove non-matching globs
SRCDIR=(~/PI/tutos/* ~/scripts/*)
if ((${#SRCDIR[@]} != 0)); then
  tar cvpzf "$FILENAME" "${SRCDIR[@]}"

You could also think of doing:

SRCDIR="$HOME/PI/tutos/* $HOME/scripts/*"
tar cvpzf "$FILENAME" $SRCDIR

As variables (such as $HOME) are expanded within double quotes, but I would advise against it as that wouldn't work properly if $HOME contains glob characters or characters of $IFS.

Category: shell script Time: 2016-07-30 Views: 0

Related post

  • bash script failing to use ssh -i. Can't see directory 2015-06-11

    I am experiencing something strange. I am working on a script to remote install Splunk on servers via a script. In this script I ssh to my practice servers (5 amazon EC2 instances running Redhat). However, when I run the script I get this error: Warn

  • How to use variable and drop in test script? 2014-05-23

    I have to modify certain Oracle Packages. Basically I will be extracting functionality and moving it to C. They provided me with some "Test" scripts that I shoud be able to run to test that my changes don't break the underlying logic of the pack

  • Using "calculate field" in python script fails to display Arabic characters correctly 2015-04-27

    Using "calculate field" in python script fails to display Arabic characters correctly. I have exported the modelbuilder to python script, but unfortunately the Arabic characters cannot by displayed correctly when the script is executed. # Import

  • Executing commands with ssh and shell script using variables on a remote machine 2014-07-02

    I'd like to execute a command and script located on a remote machine with a script on a local machine. I know it's possible to execute these kind of commands with ssh, so I made: #!/bin/bash ssh [email protected] 'cd locationOf/theScript/; ./myScript.s

  • Is it possible to use variable in sed command in unix bash script 2015-01-10

    This question already has an answer here: How to use variables in 'sed' command? 2 answers I trying to solve following problem. With great helps of others here, I can write and understand simple do loop in bash scripting. Now I try to do this: In "me

  • Why does this shell script fail in bash, but the commands work in SSH? 2014-05-09

    I'll first admit to being quite the noob in Linux/SSH/etc. What I'm trying to write is a shell script that will back up a website and all the MySQL DBs that support it. I found examples all over the 'net, but when I run the script in bash in a jailed

  • BASH script to set environment variables not working 2011-07-15

    I've written the following script to set some environment variables when needed. #!/bin/sh export BASE=/home/develop/trees echo $BASE export PATH=$PATH:$BASE echo $PATH Below the command and the results I can see on my terminal: the script runs, but

  • Unix basic shell script help needed please? 2011-08-04

    I need some help writing a basic Unix shell script that will run on AIX: Loop that will run a netstat command (netstat -an| grep 16752| grep ESTABLISHED| wc -l) that counts sessions every second and also gets the timestamp and redirects that data to

  • Can an InfoPath Form call a PowerShell script that makes use of the submitted form values? 2012-06-08

    I would like to build an InfoPath form that users can use to request a new Active Directory account (e.g. for a new employee). Once the form is submitted, I would like it to then run a PowerShell script using the supplied values from the form as para

  • require a gem in a ruby script fails at boot time 2013-06-13

    I am booting using a machine using chef. I need it to install a init.d script which it does and when the script is invoked at the end of chef run the script fails on the require 'daemons' line (shown below). I have a print before and after the requir

  • Script scraper for outputting variables and functions to a text file 2014-03-13

    I've been programming with Python 2.7 for about six months now and if possible, I'd like some critique to see what I might have done better or what I might be doing wrong. My objective was to make a script scraper that finds variables and functions,

  • SQL Injection when not using variables 2014-06-14

    Do I still need to protect my code against SQL injection when I'm not using variables in SQL queries? Is this code still vulnerable?: $result = mysqli_query($connect,"SELECT subsubcat_name FROM subsubcategories WHERE subcat_ID = 4"); while ($row

  • Failure of PHP AD LDAP script ONLY when passed variables 2016-01-22

    Sample AD LDAP Live Lookup PHP from Helpspot Okay so I am using the above sample php LDAP script to lookup user info in our ticketing system. I am running into some issues however (these are probably due to my very rudimentary understanding of PHP).

  • Using variables in Apache config files to reduce duplication? 2009-09-11

    Is it possible to use variables in Apache config files? For example, when I'm setting up a site with Django+WSGI, the config file might look like: <Directory /path/to/foo/> Order allow,deny Allow from all </Directory> Alias /foo/static /path/t

  • Why can't my Perl script see the our() variables I defined in another file? 2009-10-11

    I have a question relating to Perl and scoping. I have a common file with lots of various variables. I require the common file in my main script, but I cannot access the variables; they seem to be outside of its scope. I assumed that an our declarati

  • Is it possible to use variables in crontab -e? 2010-10-03

    Can I say: MYPATH=/root/scripts 0 1 * * * $MYPATH/$MYSCRIPT in crontab -e ? Is it possible to use variables in crontab -e ? --------------Solutions------------- Yes, you can define and use variables in this way. There's a limitation

  • How can I rewrite an old URL using variables to a static html page? 2010-10-11

    I am an htaccess newbee I am trying to redirect the old URLS for my site to the new URLS for my site. The old site used variables in the URL but the new site uses SEO friendly URLs. When I try to redirect the old URL, it is not w=redirecting and I ge

  • Does any programming language use variables as they're in maths? 2010-10-16

    In maths, a variable means you can put any number there, and an equation will still be true: root(square(x)) = abs(x) In programming languages, this is not so: a var can change. In Python: y = (x**2)**.5 x *= 2 assert y == abs(x) will raise an except

  • One-use variables - has any language ever had them? 2012-01-12

    A principle that I follow is that, when an identifier is established, it should be a signal to the reader that the value referred to is indeed an abstraction which will be used more than once. That is, the identifier is defined or assigned once, and

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 1.110 (s). 13 q(s)