Basename for directory variable

hi all,

trying to get this to work but im struggling abit and wondered if you can help me out

basically i have created a variable

base='basename $dir'

echo "please specify full path to directory you want to be made into a tar"
                        read -e dir

tar -cf "$base".tar "$dir"

basically i want to strip the full path out and just leave what the directory is called

many thanks,

rob

$base will hold the literal string basename $dir . Is that what you want? I'd guess "command substitution" would be more like what you need. Did you look into dirname ?

I'm agreeing with RudiC.

There are several quotes to consider and their uses vary. Usually in sh/ksh/bash scripts:-

  • " - Double quotes wrap a string, preserving spaces in variables, and allow the shell to work with meta-characters with the string, so variables get expanded, escaped characters have their special meaning etc. This is allowing what is known as interpolation.
  • ' - Single quotes wrap a string, preserving everything as literal text, so there is no interpolation.
  • ` - Back-quote or Back-tick is common for command substitution, as in result=`command` although aparently this is to be discouraged. I'm not sure the reasons why but I'm told that this is the preferred format - result=$(command)

There are similarly important rules for the variety of brackets, ( , [ , < , { and their appropriate opposites and a preceding $ can cause them to do yet more things.

I hope that this helps,
Robin

really sorry, should have made it clearer -

so when it asks to enter the full path to the dir, ie the end user enters -

/mnt/data/robertkwild/test

and to create the tar file it will do

tar -cf test.tar /mnt/data/robertkwild/test

I think we recognised that was the objective.

I suggest that you change your first line to one of these:-

base=$(basename $dir)            # Process substitution
base=`basename $dir`             # Back-quote/back-tick of the same
base="${dir##*/}"                # Variable substitution

Do any of these meet your needs?

A few further things:-

  • What would you expect to happen if the user keys it incorrectly or includes a trailing / in the directory input?
  • Are you aware that you can only extract a tar file created using a full path (i.e. starting with a / ) to the same place, and would that be a problem?

I hope that this helps,

Robin

im using the

base="${dir##*/}"

doesnt work, this is what i get when i run the script

[root@robw-linux /]# bash -x test.sh
+ base=
+ echo 'is this archive for an audio tar (press 1) or an audio directory (press 2)'
is this archive for an audio tar (press 1) or an audio directory (press 2)
+ read method
2
+ case $method in
+ echo 'please specify full path to directory you want to be made into a tar'
please specify full path to directory you want to be made into a tar
+ read -e dir
/to_be_archived/robstest/
+ echo 'please enter ID number ie ID1234'
please enter ID number ie ID1234
+ read id
ID67
+ echo 'please specify where you want the tar file to be stored'
please specify where you want the tar file to be stored
+ read -e dest
/archived_projects/
+ tar -cf ID67_.tar /to_be_archived/robstest/
tar: Removing leading `/' from member names
+ rsync -avh ID67_.tar /archived_projects/
sending incremental file list
ID67_.tar

sent 10.32K bytes  received 31 bytes  20.70K bytes/sec
total size is 10.24K  speedup is 0.99
+ rm -f ID67_.tar
+ rm -rf /to_be_archived/robstest/

[root@robw-linux /]

You read into the variable dir after your assign the (null) value of it to base

Move your base= line after your read -e dir in the the script and it might work better.

Robin

Yes, that's worked, I moved it under my -

read -e dir

I'm using the command I was before

base=$(basename $dir)

Thanks Robin