0

I'm trying to echo the new directory that I'm creating in the script.

BACKUP_DIR=`mkdir /tmp/"$TICKET_NUM"_EAR_BACKUP_"$(date "+%Y%m%d")"`
echo $BACKUP_DIR

But, the newly created directory is echoed in the screen. Anything Im missing here?

NewLands
  • 73
  • 2
  • 10
  • look at this question: http://stackoverflow.com/questions/4668640/how-to-execute-command-stored-in-a-variable – BigMike Sep 07 '15 at 07:09

2 Answers2

1
  1. mkdir -v seems to print out the created directory, whereas mkdir is completely silent on my systems (tested on Mac OS X and Ubuntu Linux). However, you still need to parse out the directory name from this output:
    mkdir /tmp/foo
    (no output)

    mkdir -v /tmp/foo
    mkdir: created directory `/tmp/foo'

    DIR=$(mkdir -v /tmp/foo | cut -d\  -f4- | tr -d "'\`")
    echo $DIR
    /tmp/foo

So in your case:

BACKUP_DIR=$( mkdir /tmp/"$TICKET_NUM"_EAR_BACKUP_"$(date "+%Y%m%d")" | cut -d\  -f4- | tr -d "'\`" )

  1. You might want to use the -p switch in order to create the full directory hierarchy. (Yes, /tmp will exist on MOST machines, but sometimes things can really be screwed up...).
BjoernD
  • 4,720
  • 27
  • 32
0

var=`cmd` catches output of cmd and stores in $var. But mkdir outputs nothing on success, so $BACKUP_DIR is empty.

BACKUP_DIR="/tmp/"$TICKET_NUM"_EAR_BACKUP_"$(date "+%Y%m%d")

mkdir $BACKUP_DIR

echo $BACKUP_DIR

This should work.

Terence Hang
  • 207
  • 1
  • 9