Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 29 Jun 2017, 01:44
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Copy or remove using variable with space and asterisk?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [4 Posts]  
Author Message
arivas_2005

Joined: 25 Feb 2007
Posts: 167

PostPosted: Sun 04 Jun 2017, 17:23    Post subject:  Copy or remove using variable with space and asterisk?  

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 !
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 10346
Location: Gatineau (Qc), Canada

PostPosted: Sun 04 Jun 2017, 21:21    Post subject:  

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:
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:
echo "Rabbits eat lettuce."
the sentence
Quote:
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:
echo "Wolves eat rabbits."
, you have to write
Code:
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.
Rabbits-do-not-eat-wolves.jpg
 Description   (Sorry for the typo.)
 Filesize   28.8 KB
 Viewed   90 Time(s)

Rabbits-do-not-eat-wolves.jpg


_________________
musher0
~~~~~~~~~~
« Un insensé sur le trône n'est qu'un singe sur le haut d'un toit. » / "A madman
on the throne is just a monkey on top of a roof." (Bernard de Clervaux)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1279
Location: Japan

PostPosted: Sun 04 Jun 2017, 23:57    Post subject: Re: Copy or remove using variable with space and asterisk?  

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

Try this:
Code:
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:
TMP=$IFS; IFS=
rm $filet
IFS=$TMP

 

 

Last edited by MochiMoppel on Yesterday, at 03:47; edited 1 time in total
Back to top
View user's profile Send private message 
arivas_2005

Joined: 25 Feb 2007
Posts: 167

PostPosted: Mon 05 Jun 2017, 23:15    Post subject:    

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:
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 Very Happy
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [4 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0519s ][ Queries: 15 (0.0098s) ][ GZIP on ]