Copy or remove using variable with space and asterisk?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

Copy or remove using variable with space and asterisk?

#1 Post by arivas_2005 »

Greetings !
I can not copy file to group using variables (using asterisk)

I have

files in folder dirOr:
la le li.jpg
la lo li.jpg
lu le li.jpg

# variables
dirOr="/Z seccion"
dirDs="/S seccion"
file1="/Z seccion/la le li.jpg"
filet="/Z seccion/*li.jpg"

a) copy ---------------
cp -- "$file1" "$dirDs"
# Using the name of each file I can copy OK
but
Using asterisk
cp -- "$filet" "$dirDs"
show message
cp: cannot stat ‘/Z s seccion/*li.jpg’: No such file or directory

How can I copy into groups using an asterisk in the variable ?


b) remove? ---------------------
rm -- "$file1" ## remove OK
but
rm -- "$filet"
show message
rm: unrecognized option '--/Z s seccion/*li.jpg'

Thanks !

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Hello arivas_2005.

Off the top of my head:

-- do you absolutely need the double-dash on the cp command line? If you
don't need it, it can get confusing to use it.

-- As you wrote it in your message above, "/Z section" means a directory
named "Z section" at the very top ("/") of your Linux directories. Is this
really the case?

Any copy of your variable for "/Z section" will fail because only one
directory can be named "Z section" at "/". The cp command will normally
refuse to copy any file or directory over itself.

Another reason why we avoid diagonal bars in file names and variables is:
-- the right-sloped diagonal bar ("/") is used by the Linux and Unix
systems as delimiter for directories. For ex, you will type

Code: Select all

cd /usr/local/bin
to go to that directory. Please note the the first right-sloped diagonal bar
("/") represents the very top of the directories and that the other ones tell
Linux where to fork to the sub-directories.

-- the left-sloped diagonal bar ("\") is considered a warning sign in bash
scripts and perhaps other computer languages for the quotation mark and
similar characters.

For ex., at a terminal if you type

Code: Select all

echo "Rabbits eat lettuce."
the sentence
Rabbits eat lettuce.
will be written on the next line of your terminal.

If at the same terminal you want to see the command

Code: Select all

echo "Wolves eat rabbits."
, you have to write

Code: Select all

echo "echo \"Wolves eat rabbits.\""
~~~~~~~~~
Same comments for your directory "/S section".
~~~~~~~~~

-- In Linux and Unix we try to avoid spaces in filenames. The newer Linux
kernels are able to understand the space, between quotation marks, for
ex. file "a b c". But it is always better in Linux, IMO, in order to avoid
confusion and potential problems, to keep the old practice of replacing the
space with the underscore, for ex., to rename file "a b c" as file "a_b_c".

To this end your Puppy has a rename tool. Look for it in your Puppy menu
under the Utilities or File_Management sections.

One other rename tool that I like is "qmv", at
http://www.nongnu.org/renameutils.
It lets you use your text editor to rename files, which makes it very easy.

IHTH.
Attachments
Rabbits-do-not-eat-wolves.jpg
(Sorry for the typo.)
(28.8 KiB) Downloaded 338 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

Re: Copy or remove using variable with space and asterisk?

#3 Post by MochiMoppel »

arivas_2005 wrote:How can I copy into groups using an asterisk in the variable ?
Try this:

Code: Select all

TMP=$IFS; IFS=
cp $filet $dirDs
IFS=$TMP
arivas_2005 wrote:rm -- "$filet"
show message
rm: unrecognized option '--/Z s seccion/*li.jpg'
No, it doesn't. Your code will fail, but not with the message you posted. You get this message when you forget to put a space between -- and "$filet"

To remove the files in $filet you again have to temporarily reset the IFS variable so that bash does no split filenames at space characters:

Code: Select all

TMP=$IFS; IFS=
rm $filet
IFS=$TMP
 

 
Last edited by MochiMoppel on Wed 28 Jun 2017, 07:47, edited 1 time in total.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#4 Post by arivas_2005 »

Thanks MochiMoppel
It's a very fine solution
Thank you again

Almost at the same time and After hours of experiment and google built an option.
But it is very hard ( and the way of MochiMoppel is very easy)

Place the * outside the string
Example:

Code: Select all

dirDs="/S seccion"
filet2="/Z seccion/*li.jpg"

# Cut into the *  --> produces 2 substrings

cadeizq=${filet2%%"*"*}
cadeder=${filet2##*"*"} 

#    -->  put out the *
cp "$cadeizq"*"$cadeder" "$dirDs"

#idem for remove
#rm "$cadeizq"*"$cadeder" 
Thank you again MochiMoppel :D

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#5 Post by arivas_2005 »

regards

I relive the problem of using parameters in variable.
I have again the problem of inserting parameters with * such as -name "* .txt" as variable of find ./ $ parameter,
Example:

Code: Select all

CONDITION=" maxdepth 1  -type f -name "*.txt""
find ./ $CONDITION
but the asterisk can not make it work.
The IFS technique suggested above, does not work for me.
'----------------------------------------------------
also parameters like the following: grep .txt | grep FORM THE PROBLEM IS THE |
Or in grep '.txt\|FORM'too, is \|
Example2:

Code: Select all

CONDITION=" maxdepth 1  -type f"
CONDITION2=" | grep .txt | grep FORM"
find ./ $CONDITION $CONDITION2
'--------------------------------------------------
also and tried to use the content of the URL
http://murga-linux.com/puppy/viewtopic. ... c5455d1e3e
but I can not master the technique of expressions


Thanks for your considerations :oops:

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#6 Post by MochiMoppel »

CONDITION=" -maxdepth 1 -type f -name \"*.txt\""
eval find ./ $CONDITION

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#7 Post by arivas_2005 »

Thanks for the prompt response
I comment. use find ver 4.4.2
and

the segment -name \"*.txt\"" in
eval find ./ $CONDITION
not work in my computer. I use tahrpup version 6.0.6
error:
find: ` -maxdepth 1 -type f -name "*.txt"': No such file or directory

and shows the entire list without filter only txt
my lab script is:

Code: Select all

#!/bin/bash
cd /mnt/sde1/Z_SECCION-2GD=sacarnotadeFORMAS/4
## CONDITION=" -maxdepth 1 -type f -name \"*.txt\"" # for reference
CONDITION=`Xdialog --stdout --separator "|" --inputbox "parametro para find " 8 125 ""`
eval find ./ "$CONDITION" > tempo.txt

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#8 Post by MochiMoppel »

It works for the example you posted and asked help for.
In the example you posted you must surround the stuff after '=' with quotes and you must escape the double quotes within the surrounding double quotes.

If you use Xdialog you must not do this.
When using Xdialog inputbox this would result in the error you mentioned:

Code: Select all

" -maxdepth 1  -type f -name \"*.txt\""
This is how the Xdialog input should look like:

Code: Select all

-maxdepth 1  -type f -name "*.txt"

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#9 Post by arivas_2005 »

my sincere thanks for the support!

Post Reply