Basename for directory variable


 
Thread Tools Search this Thread
Top Forums Programming Basename for directory variable
# 1  
Old 12-07-2016
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
# 2  
Old 12-07-2016
$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?
# 3  
Old 12-07-2016
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 12-07-2016
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

# 5  
Old 12-07-2016
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
# 6  
Old 12-07-2016
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 12:53 PM..
# 7  
Old 12-07-2016
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning basename result to another variable

This is a two part request for an assistance. I am not sure how retrieve value from basename command - line 270 -so in can be output as variable CLI_COMMAND - line 250 in whiptail input box. As coded I can input from keyboard ( stdin?) into input box using redirection. I can... (2 Replies)
Discussion started by: annacreek
2 Replies

2. Shell Programming and Scripting

$(basename $0)

what is the meaning of "script_name=$(basename $0)", can someone please explain? (1 Reply)
Discussion started by: abhi200389
1 Replies

3. Shell Programming and Scripting

Basename in subshell

Hi All, I would like to improve my bash scripting skill and found a problem which I do not understand. Task is to search and print files in directory (and subdirecories) which contains its own name. Files can have spaces in name. This one works fine for files in main directory, but not for... (4 Replies)
Discussion started by: new_item
4 Replies

4. UNIX for Dummies Questions & Answers

awk and basename

im trying to extract the basename of a process running on a host processx is running at host1 as /applications/myapps/bin/processx i wanted to check if its running, then extract the basename only using: $ ssh host1 "ps aux | grep -v 'grep' | grep 'processx'" | awk '{ print basename $11}' ... (10 Replies)
Discussion started by: kaboink
10 Replies

5. UNIX for Dummies Questions & Answers

basename

Hi, can anyone let me know how to interpret the below third line in the following code. Gone through the man pages of "basename", but no go. for f in *.foo; do base=`basename $f .foo` mv $f $base.bar done Thanks. (2 Replies)
Discussion started by: venkatesht
2 Replies

6. Shell Programming and Scripting

basename $0

hi, can anyone help me by saying what is basename.. i have seen this in many programs where the basename is used.... thanks, Krips (4 Replies)
Discussion started by: kripssmart
4 Replies

7. Shell Programming and Scripting

basename problem

Hi guys if i do a=`basename -e -s /home/j/john/*` du -k -h $a | sort -nr | head -10 why when i run the script does it work but also say usage basename string any ideas thanks (9 Replies)
Discussion started by: musicmancanora4
9 Replies

8. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

9. Shell Programming and Scripting

Setting basename and dirname variable to simply script.

Hello all, Can somebody explain to me how set up a basename and dirname variable to simplify this script. I currently have a 'infile' with the contents of FTTPDataPVC_ & BaaisDSLFeed. I need to add a basename and or dirname variable so that any additions can be made through the infile and not... (1 Reply)
Discussion started by: liketheshell
1 Replies

10. Shell Programming and Scripting

reverse of basename

Hi, Can someone let me know how to find the reverse of the basename i.e i have /apps/tiv/pmon/xxxx.dat and I want /apps/tiv/pmon/ Thanks (7 Replies)
Discussion started by: braindrain
7 Replies
Login or Register to Ask a Question