Boat Drinks  

Go Back   Boat Drinks > General > Computer and Consoles

Reply
 
Thread Tools Display Modes
Old 26-02-2010, 21:30   #1
Desmo
The Last Airbender
 
Desmo's Avatar
 
Join Date: Jun 2006
Location: Pigmopad
Posts: 11,915
Default Script help....again :)

Been searching for an answer on this but it's all a bit over my head at the moment.

I have one directory with multiple directories inside it. I need to run the command "dpkg -b <directory name>" to create a deb package of that directory. Thing is, I want to be able to run it automatically for whatever directories are present. The command can also only handle one directory at a time.

So, I need a script that will collect the directory names and then pass them on to that command one at a time until they are all done.

__________________
Desmo is offline   Reply With Quote
Old 26-02-2010, 22:05   #2
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

Ta-da:

find . -type d -exec dpkg -b {} \;

'Find everything, in the current directory (.), of type directory (-type d) and execute (-exec) the command dpkg -b'. The bits at the end are find's way of substituting parameters, so the directory will go where the {} is, and the \; signifies the end of the command.

Last edited by Mark; 26-02-2010 at 22:10.
Mark is offline   Reply With Quote
Old 26-02-2010, 22:15   #3
Desmo
The Last Airbender
 
Desmo's Avatar
 
Join Date: Jun 2006
Location: Pigmopad
Posts: 11,915
Default

Cheers Mark, that seems to have done the trick although it's not quite perfect

Can you make it so it only does the directories in the main directory? What it seems to be doing is running the command on as many sub-directories as it can find. Whilst this doesn't actually cause a problem as it can't find the control file to create the deb, it just gives a messy output.

But thanks again as it does give the final files I need
__________________
Desmo is offline   Reply With Quote
Old 26-02-2010, 22:25   #4
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

find . -maxdepth 1 -type d -exec dpkg -b {} \;

You might need -maxdepth 2. I rarely get that one right.
Mark is offline   Reply With Quote
Old 26-02-2010, 22:32   #5
Desmo
The Last Airbender
 
Desmo's Avatar
 
Join Date: Jun 2006
Location: Pigmopad
Posts: 11,915
Default

depth 1 worked a treat
__________________
Desmo is offline   Reply With Quote
Old 27-02-2010, 02:23   #6
Garp
Preparing more tumbleweed
 
Garp's Avatar
 
Join Date: Jun 2006
Location: Hawaii
Posts: 6,038
Default

To cover a geek angle it's worth emphasising the importance of -exec there.
Linux allows you to have filenames with spaces and other special characters in them, e.g.
test file here

to view or access the file those spaces need to be 'escaped' (preceeded by a \ symbol), e.g.
Code:
$ cat test\ file\ here
otherwise Linux would be looking for the three files "test", "file" and "here".

It's quite a common trick to pipe responses into xargs, e.g.
Code:
find . -name "*here"| xargs cat
With normal file names that have no special characters that would work fine. But in this case we get:
Code:
# find . -name "*here"  | xargs cat
cat: ./test: No such file or directory
cat: file: No such file or directory
cat: here: No such file or directory
That's fine for "cat" processes, if it fails no big deal but if you using "rm -rf" or similar it can be disastrous as you might end up deleting the wrong directory (seen it done, sadly).

-exec gets away with all that fuss, it makes sure whatever commands are carried out actually take place against the correct file / directory, leaving no room for mistakes (unless your entire concept is bad, but there is no helping that!)
Code:
# find . -name "*here" -exec cat {} \;
Boat Drinks
__________________
Mal: Define "interesting"?
Wash: "Oh, God, oh, God, we're all gonna die"?
Garp is offline   Reply With Quote
Old 27-02-2010, 10:28   #7
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

And the other Geek angle is why *here was in quotes. Try it without (as all too often happens), and one of three things will happen:
  1. If there are no files that match the pattern (i.e. end in 'here') in the current directory, it'll 'just work'. You then assume it'll always work, but...
  2. If there is exactly one file that matches, e.g. 'test file here', it'll only match files with that exact name. Probably not what you intended.
  3. If there is more than one file that matches, the command will fail. Definitely not what you wanted.
Mark is offline   Reply With Quote
Old 27-02-2010, 15:12   #8
Desmo
The Last Airbender
 
Desmo's Avatar
 
Join Date: Jun 2006
Location: Pigmopad
Posts: 11,915
Default

Cheers guys

It's always nice to get a bit more info about these things just so I know why something is happening rather than just blindly copying it.

Just so you know what it's for, my iPhone repo is now totally automatic
I drop apps or themes in to folders and then run a script based on what repo it should go in to. It now automatically creates the deb based on all folders, creates a packages file, creates a zip of the packages file and uploads all of that to the webspace all with one click of a button
__________________
Desmo is offline   Reply With Quote
Reply


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
Forum Jump


All times are GMT +1. The time now is 15:48.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.