0
  • $1 is file / folder that want to compressed
  • Output filename is the same name, plus current date and ext
  • if output name exist, then just give warning

Example:

./cmp.sh /home/user

It will be /home/user to /home/user_2018-03-11.tar.bz2

i already have lead, but i'm stuck

#!/bin/bash

if ["$1" == ""]; then
echo "Help : To compress file use argument with directory"
exit 0
fi


if [[ -f "$1" || -d "$1" ]]; then
tar -cvjSf $1"_"$(date '+%d-%m-%y').tar.bz2 $1
fi

but the output is _22-04-2018.tar.bz2

  • 1
    Is the problem the missing spaces? `if ["$1" == ""]; then` should be `if [ "$1" == "" ]; then` – user000001 Apr 22 '18 at 09:22
  • sorry the first if is just help command for something like (-?) – Davin Dirgantara Apr 22 '18 at 09:24
  • *"... but i'm stuck"* is not a good problem statement. You need to explain the problem or state the errors. Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Apr 22 '18 at 12:14
  • 1
    Possible duplicate of [Shell spacing in square brackets](https://stackoverflow.com/q/19032680/608639), [What's the difference between [ and [[ in Bash?](https://stackoverflow.com/q/3427872/608639), [How to use double or single brackets, parentheses, curly braces](https://stackoverflow.com/q/2188199/608639), [How to check if a variable is set in Bash?](https://stackoverflow.com/q/19032680/608639), etc. – jww Apr 22 '18 at 12:19

2 Answers2

3

I see that you're using quotes to avoid the problem the underscore getting used as part of the variable name. So while $1 is a positional paramater, $1_ is a variable that you have not set in your script. You can avoid this issue by using curly braces, like ${1}. Anything inside the braces is part of the variable name, so ${1}_ works. This notation would be preferable to $1"_" which leaves a user-provided variable outside of quotes. (Of course, "$1"_ would do the job as wel.)

Also, it's probably safer to set the filename in a variable, then use that for all your needs:

#!/bin/bash

if [ -z "$1" ]; then
  echo "Help : To compress file use argument with directory"
  exit 0
fi

filename="${1}_$(date '+%F').tar.bz2"

if [ -e "$filename" ]; then
  echo "WARNING: file exists: $filename" >&2
else
  tar -cvjSf "$filename" "$@"
fi

Changes:

  • you need spaces around your square brackets in an if condition,
  • while you can test for equivalence to a null string, -z is cleaner, though you could also test for [ $# -eq 0 ], counting the parameters provided,
  • using $filename makes sure that your test and your tar will always use the same name, even if the script runs over midnight, and is way more readable,
  • variables should always be quoted.

Also, are you sure about the -S option for tar? On my system, that option extracts sparse files, and is only useful in conjunction with -x.

ALSO, I should note that as I've rewritten it, there's nothing in this script which is specific to bash, and it should be portable to POSIX shells as well (ash/dash/etc). Bash is great, but it's not universal, and if through your learning journey you can learn both, it will give you useful skills across multiple operating systems and environments.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • Much Appreciated for your answer @ghoti, do you know any good books for basic scripting so i can have strong fundamental first ? – Davin Dirgantara Apr 22 '18 at 09:50
  • 1
    @DavinDirgantara, my pleasure. I'm afraid I can't help much with printed book recommendations, other than to say that anything from O'Reilly is likely to be good. But read everything at [GreyCat's wiki](https://mywiki.wooledge.org/) and don't take tdlp.org as gospel. Ask lots of questions! We love questions! :-) – ghoti Apr 22 '18 at 09:56
1

Use -z switch to check if blank

#!/bin/bash 
if [[ -z "$1" ]]; then 
  echo "Help : To compress file use argument with directory" 
  exit 0 
fi 
if [[ -f "$1" || -d "$1" ]]; then 
  tar -cvjSf $1"_"$(date '+%d-%m-%y').tar.bz2 $1 
fi
Mesar ali
  • 1,832
  • 2
  • 16
  • 18