LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   This should be a simple script (and it would be nice if it worked) (https://www.linuxquestions.org/questions/linux-newbie-8/this-should-be-a-simple-script-and-it-would-be-nice-if-it-worked-4175469932/)

taylorkh 07-17-2013 08:43 AM

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

Philip Lacroix 07-17-2013 09:45 AM

Hi,

Quote:

Originally Posted by taylorkh (Post 4991940)
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 (Post 4991940)
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

jpollard 07-17-2013 09:49 AM

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)

Firerat 07-17-2013 10:08 AM

you could get find to execute it

Code:

find . -type f -name "*.001" -exec lxsplit -j {} ';'

taylorkh 07-17-2013 10:55 AM

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

Firerat 07-17-2013 11:09 AM

basically, yes and yes

{} is the 'found' file/dir

man find
has more detail

taylorkh 07-17-2013 11:37 AM

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

Firerat 07-17-2013 11:45 AM

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

suicidaleggroll 07-17-2013 12:52 PM

Quote:

Originally Posted by Firerat (Post 4992033)
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).

taylorkh 07-17-2013 05:23 PM

My thanks to all who contributed. I have now joined my files back together and have learned some more scripting skills.

Regards,

Ken


All times are GMT -5. The time now is 01:32 PM.