LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-17-2013, 08:43 AM   #1
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Rep: Reputation: 174Reputation: 174
This should be a simple script (and it would be nice if it worked)


Pardon me if I am taking the MVS JCL approach to building this script (I have only known 3 persons who COULD write JCL from scratch and only one who WOULD do so. The normal approach was to take someone else's script and modify it :-).

I have a bunch of files which were split with HJ Split or something similar. I have found and installed lxsplit which allows me to join them from the command line thusly:

lxsplit -j mysplitfile.001

where the .001 file is the first in the set. Works great except I have a number of files to join and they have spaces in the names. So I pulled out a little script which vacuums or compacts Firefox database files as a starting example.
Quote:
for i in *.sqlite; do echo "VACUUM;" | sqlite3 $i ; done
I modified it thusly
Quote:
for i in *.001; do echo "lxsplit -j" \"$i\" ; done
This in fact displays the proper commands to the terminal. However, it does not execute them.
Quote:
[ken@taylor12 numbered]$ for i in *.001; do echo "lxsplit -j" \"$i\" ; done
lxsplit -j "my first file.djvu.001"
lxsplit -j "my second file.pdf.001"
lxsplit -j "my third file.djvu.001"
If I direct the output to a file and execute it as a script it performs the desired joins. The question is... How do I make the script execute the desired commands in the terminal?

The first script pipes the command VACUUM to the sqlite program. In my case I am not executing any commands within lxsplit. I am simply running the program with a couple of arguments. I tried this
Quote:
[ken@taylor12 numbered]$ for i in *.001; do lxsplit -j < \"$i\" ; done
bash: \"$i\": ambiguous redirect
but no luck. Same error without the escaped quotes. Finally I tried
Quote:
for i in *.001; do lxsplit -j echo \"$i\" ; done
which produced no errors but also did nothing.

I am obviously missing something simple. Can anyone please clue me in?

TIA,

Ken
 
Old 07-17-2013, 09:45 AM   #2
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware
Posts: 441

Rep: Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574Reputation: 574
Hi,

Quote:
Originally Posted by taylorkh View Post
for i in *.001; do echo "lxsplit -j" \"$i\" ; done
I'm not a bash scripting guru but this is not supposed to execute lxsplit: it will run the echo command with your string as an argument.

Quote:
Originally Posted by taylorkh View Post
for i in *.001; do lxsplit -j echo \"$i\" ; done
Here you are passing the echo command as an argument to lxsplit, which I don't think is supposed to work either.

I have never used lxsplit, but according to the project homepage this is how the syntax should be, in order to merge the split pieces into the original file:

Code:
lxsplit -j smallfiles.bin.001
Therefore I would try with this:

Code:
for i in *.001 ; do lxsplit -j "${i}" ; done
Best regards,
Philip
 
Old 07-17-2013, 09:49 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
bad time for escaping ]

Try:
Code:
for i in *.001; do lxsplit -j "$i" ; done
I believe the " in the file name confused things - removing the escaping will allow the scanner to do it thing (which removes the " characters) leaving just the file name.

When you were testing, the part '...do echo "lxsplit -j" \"$i\" ' had proper escaping for embedding a " in the output... but you then removed the echo and the quotes around the lxsplilt -j.. but forgot to also remove the escape from the rest.

I also missed the erroneous redirect... (sorry about that)

Last edited by jpollard; 07-17-2013 at 09:50 AM. Reason: I had a booboo.. pointed out by the earlier post.
 
Old 07-17-2013, 10:08 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
you could get find to execute it

Code:
find . -type f -name "*.001" -exec lxsplit -j {} ';'
 
2 members found this post helpful.
Old 07-17-2013, 10:55 AM   #5
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks Firerat, that did the trick. If I might impose on you for a little help in understanding...

The find command I understand. The exec part I think I understand and {} must be passing the name of the file to lxsplit. What I do not understand is how/why this loops through all of the found files in the directory. Does the -exec cause the command to be executed for each result of the find command?

Thanks again,

Ken
 
Old 07-17-2013, 11:09 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
basically, yes and yes

{} is the 'found' file/dir

man find
has more detail
 
Old 07-17-2013, 11:37 AM   #7
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks Firecat. I will have a look at the man page. I have used find to "find" files. Looks like it can do a lot more magic.

Ken
 
Old 07-17-2013, 11:45 AM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
just thought I would throw in a 'pure' bash solution

Code:
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for File in *.001;do
    lxsplit -j "$File"
done

IFS=$SAVEIFS
unset SAVEIFS
This works by changing the IFS ( Internal field separator ) , thus the spaces in file/dir name do not cause the 'word splitting'.
http://tldp.org/LDP/abs/html/internalvariables.html
 
Old 07-17-2013, 12:52 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by Firerat View Post
just thought I would throw in a 'pure' bash solution

Code:
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for File in *.001;do
    lxsplit -j "$File"
done

IFS=$SAVEIFS
unset SAVEIFS
This works by changing the IFS ( Internal field separator ) , thus the spaces in file/dir name do not cause the 'word splitting'.
http://tldp.org/LDP/abs/html/internalvariables.html
All of the IFS stuff is unnecessary, a bure bash solution was already posted earlier by jpollard:
Code:
for i in *.001; do lxsplit -j "$i" ; done
"for i in *" handles spaces just fine, and the quotes around $i in the lxsplit call keep it from splitting there. "find" works, but is grossly overkill for this problem. A --maxdepth=1 parameters should be added in the find command as well to keep it from diving into subdirectories (assuming that behavior is not desired).

Last edited by suicidaleggroll; 07-17-2013 at 12:53 PM.
 
Old 07-17-2013, 05:23 PM   #10
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
My thanks to all who contributed. I have now joined my files back together and have learned some more scripting skills.

Regards,

Ken
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Alarm Clock - Nice Simple App for Setting Reminders and Alarms in Ubuntu LXer Syndicated Linux News 0 11-05-2010 06:50 PM
Is 'nice' inherited to child processes? e.g. bash script/php script that calls MySQL SirTristan Linux - Newbie 1 12-04-2008 12:57 AM
A nice, simple, easy distro? tauhshi Linux - General 5 11-27-2006 07:27 PM
can't use simple firewall script (it worked before) tigerflag Linux - Security 2 06-23-2003 12:10 AM
Fluxbox would be nice if it worked. Crashed_Again Linux - General 10 02-13-2003 11:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:32 AM.

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