Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Basename for directory variable

Programming


Tags
solved

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-07-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 15 September 2017, 6:29 PM EDT
Posts: 71
Thanks: 7
Thanked 5 Times in 4 Posts
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


Code:
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
Sponsored Links
    #2  
Old Unix and Linux 12-07-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 September 2017, 7:18 AM EDT
Location: Aachen, Germany
Posts: 11,342
Thanks: 296
Thanked 3,520 Times in 3,239 Posts
$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?
Sponsored Links
    #3  
Old Unix and Linux 12-07-2016
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 15 September 2017, 11:35 AM EDT
Location: Lancashire, UK
Posts: 3,256
Thanks: 1,389
Thanked 630 Times in 569 Posts
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
    #4  
Old Unix and Linux 12-07-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 15 September 2017, 6:29 PM EDT
Posts: 71
Thanks: 7
Thanked 5 Times in 4 Posts
really sorry, should have made it clearer -

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

Code:
/mnt/data/robertkwild/test

and to create the tar file it will do

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

Sponsored Links
    #5  
Old Unix and Linux 12-07-2016
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 15 September 2017, 11:35 AM EDT
Location: Lancashire, UK
Posts: 3,256
Thanks: 1,389
Thanked 630 Times in 569 Posts
I think we recognised that was the objective.

I suggest that you change your first line to one of these:-
Code:
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
Sponsored Links
    #6  
Old Unix and Linux 12-07-2016
robertkwild robertkwild is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 15 September 2017, 6:29 PM EDT
Posts: 71
Thanks: 7
Thanked 5 Times in 4 Posts
im using the

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

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


Code:
[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 /]

Last edited by rbatte1; 12-07-2016 at 11:53 AM..
Sponsored Links
    #7  
Old Unix and Linux 12-07-2016
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 15 September 2017, 11:35 AM EDT
Location: Lancashire, UK
Posts: 3,256
Thanks: 1,389
Thanked 630 Times in 569 Posts
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
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
$(basename $0) abhi200389 Shell Programming and Scripting 1 12-25-2012 01:26 AM
basename venkatesht UNIX for Dummies Questions & Answers 2 03-31-2010 06:27 AM
basename $0 kripssmart Shell Programming and Scripting 4 05-12-2009 05:13 AM
Sed variable substitution when variable constructed of a directory path alrinno Shell Programming and Scripting 2 07-11-2008 02:24 PM
Setting basename and dirname variable to simply script. liketheshell Shell Programming and Scripting 1 09-12-2007 01:36 PM



All times are GMT -4. The time now is 07:48 AM.