LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-17-2011, 08:49 AM   #1
farmerdan
Member
 
Registered: Feb 2004
Location: IA-MO State Line
Distribution: Ubuntu 10.04
Posts: 59

Rep: Reputation: 15
Changing File Names to Include Part of the Directory Name


I want to do something similar to what I found in this thread. I have scripts to compile and install packages, all names "build," that are in directories named for the package.

/shadow-4.1.4.3/build
/module-init-tools-3.12/build

I want to change the name of the files to

shadow-build
module-init-tools-build

I need to add that this is the first time I've used `cut` so I'm just learning it.

I can achieve what I want if I have two cut statements in a loop. The first is
Code:
newname=$(echo $dir | cut -f1 -d.)
which gives, for example shadow-4. I can reduce this to only "shadow" by passing
Code:
-d-
in a second command to `cut`, but that doesn't work so well with packages like "module-init-tools" that have a series of fields separated by "-". And packages have a variable number of fields like this.

Currently, only the "two command" method gives me what I want. I'm hoping someone can suggest or point me to something that can reduce this to only one command.

Thanks,
Dan
 
Old 03-18-2011, 02:15 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
You could use bash parameter expansion:
Code:
filename_prefix=${dir%%-*}
 
1 members found this post helpful.
Old 03-18-2011, 09:45 AM   #3
farmerdan
Member
 
Registered: Feb 2004
Location: IA-MO State Line
Distribution: Ubuntu 10.04
Posts: 59

Original Poster
Rep: Reputation: 15
Thanks. I had discovered that expansion last night and was playing with it. But you have suggested a much simpler substitution. I was using [-0-9][0-9]. Yours is much simpler.

The situation now is that I cannot get it to run in a loop.

If in a script or straight to a terminal, or script without a loop, I run
Code:
dir=shadow-4.1.4.3
echo $dir
pattern=-*
echo $pattern
newname=${dir%%$pattern}
echo $newname
The return for $newname is "shadow."

However, if I enclose those statements in a for loop,
Code:
for dir in shadow-4.1.4.3 module-init-tools-3.12
<same statements as above except I don't set $dir
done
The return is "pattern: command not found." It's as if there's white space around "=" when "pattern" is defined.

It's got to be something simple--it usually is, especially when using the commands directly works.

Thoughts?

Thanks again.
Dan
 
Old 03-18-2011, 10:08 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It works for me:
Code:
for dir in shadow-4.1.4.3 module-init-tools-3.12
do
  pattern=-*
  echo $pattern
  newname=${dir%%$pattern}
  echo $newname
done
produces:
Code:
-*
shadow
-*
module
even if it would be safer to enclose -* in single quotes or use it directly in the parameter substitution as catkin showed. Please, write down the exact statements that give this error.
 
1 members found this post helpful.
Old 03-18-2011, 11:03 AM   #5
farmerdan
Member
 
Registered: Feb 2004
Location: IA-MO State Line
Distribution: Ubuntu 10.04
Posts: 59

Original Poster
Rep: Reputation: 15
In moving around in terminals, I can't find the commands that cause the error. But here's a copy of the script that I was testing which produces the errors:
Code:
! /bin/bash

# A script to remove all but the actual names of a package.
# shadow-4.1.4.3 --> shadow

pattern=-*
echo "pattern =" $(pattern)

for dir in `ls /home/dan/LFS-6.8/cut-test`
do
echo $dir
newname=${dir%%$pattern}
echo $(newname)
done
I copied and pasted your commands into a terminal and all was well, and then ran them in a script. The script succeeded. I did learn, when "module-init-tools-3.12" became "module" I needed to use '%' instead of '%%'. It's these "naggy" mistakes that cause me frustration and I'm sure I did something like that in the script and the commands that I was using in the terminal.

The only difference I see between things now is that $parameter is declared outside the loop instead of inside of it. I've declared variables before outside loops, but I've never encountered something like this until now. Am I missing something conceptual or can you see a "typo" that I'm missing.

At any rate, since scripting works, I can proceed with my project.

Thanks very much.

Dan

Last edited by farmerdan; 03-18-2011 at 11:05 AM.
 
Old 03-18-2011, 11:26 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
echo "pattern =" $(pattern)
Here is the offending line. pattern is inside command substitution, therefore it is interpreted as a command and it's not found by the shell. The correct syntax for variable reference (or parameter substitution) is
Code:
echo "pattern =" ${pattern}
The same for echo $(newname) below. Just a typo, I guess.
 
Old 03-18-2011, 11:47 AM   #7
farmerdan
Member
 
Registered: Feb 2004
Location: IA-MO State Line
Distribution: Ubuntu 10.04
Posts: 59

Original Poster
Rep: Reputation: 15
Hmmmmmm! More than just a typo. Conceptual error. I didn't quite understand your remark
Quote:
pattern is inside command substitution
so I changed $(pattern) and $(newname) to $pattern and $newname, ran the script and got the desired results.

I've never been quite clear on the use of ( ) and { } in variables and patterns. I've just used what works. Looks like I'll be nosing around in the "The Bash Reference Guide" and "The Advanced Scripting Guide" again today.

Thank you very much for your help.

Dan
 
Old 03-18-2011, 12:02 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In a few words: when you reference a variable (that is you want to retrieve the value of that variable) the correct syntax is:
Code:
${variable_name}
but you can omit the brackets, except in some cases where they are mandatory to obtain the desired result. For example if you want to embed the value of the variable in a string you have to tell the shell where the variable name terminates, otherwise any other valid character is considered part of the name itself. Example: suppose you have a variable called my_var and you want to embed its value in a string:
Code:
echo this_contains_the_value_$my_var_change_it_and_this_string_will_change
This stupid example, just show you that the shell tries to reference a variable called my_var_change_it_and_this_string_will_change, whereas if you use brackets:
Code:
echo this_contains_the_value_${my_var}_change_it_and_this_string_will_change
the shell references the variable my_var correctly.

Regarding command substitution, it is another story. It serves to substitute the result of a command, not the value of a variable:
Code:
echo Today is $(date)
will substitute the result of the date command, not the value of an hypothetical variable called date.
Indeed, you will find a lot of details on the aforementioned documentation.

Last edited by colucix; 03-18-2011 at 12:03 PM.
 
1 members found this post helpful.
Old 03-18-2011, 02:08 PM   #9
farmerdan
Member
 
Registered: Feb 2004
Location: IA-MO State Line
Distribution: Ubuntu 10.04
Posts: 59

Original Poster
Rep: Reputation: 15
colucix, thank you so much for this. It is the best and most understandable discussion of variables and substitution that I've ever encountered. I see now why I wasn't getting the results I wanted.

This is, in fact, so important to me that I'm going to "bookmark" this response so that I can refer to it in the future when I have questions.

I just noticed what you have in your "signature block."
Quote:
Complex problems have simple, easy to understand, wrong answers. (Henry Louis Mencken)
This appeals to me since I'm a great fan of Occam's Razor--the simplest solution is almost always the best. But then the situation is in identifying the simplest solution and that's where my signature block comes into play.
Quote:
I don't know what I don't know
Merging these concepts can be terribly annoying.

Thanks again. I hope our paths cross once more.

Dan
 
Old 03-18-2011, 02:57 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome!

I changed my signature recently. Maybe you like my previous one:

Theory is when you know all and nothing works. Practice is when all works and nobody knows why. In this case we have put together theory and practice: nothing works... and nobody knows why! (Albert Einstein)

And I am an estimator of the Occam's Razor principle, too!
 
Old 03-18-2011, 03:07 PM   #11
farmerdan
Member
 
Registered: Feb 2004
Location: IA-MO State Line
Distribution: Ubuntu 10.04
Posts: 59

Original Poster
Rep: Reputation: 15
Your previous signature----------

Been there. Done that. Got the T-shirt.

I do like it.

Dan
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to changing same file names by their directory names? bayaraa_u Linux - General 3 04-09-2010 08:26 AM
changing file names scofiled83 Programming 8 05-23-2009 05:31 AM
Need script for changing DIRECTORY names only to upper case PaxRomana Programming 15 05-08-2007 05:04 AM
#include <task.h> : No such file or directory Eileen Programming 1 04-20-2006 03:26 AM
Changing global file names. Sapient Linux - General 6 03-31-2005 05:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration