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 09-29-2012, 12:22 PM   #1
mirzasoltan
LQ Newbie
 
Registered: Oct 2010
Posts: 28

Rep: Reputation: 0
help for script


hi all,
i have a problem with my script. hope someone can help me.

i have a many files that named with space characters, like "test test.mp3"

in bash when i want copy this file bash help me to complete the name like this:
"cp ./test\ test.mp3 /tmp" (i know that "\" character means that bash dont see the next character and for this example bash will ignore space character after test)

here is my problem, that i want to use variable for this in my script to copy this type of named files (i mean with space characters) automatically.

when i use this type these happen:

[root@localhost ~]# C=$(grep -wi "MP3" /tmp/sync | awk -F: '{print $1}'|head -n3|tail -n1)
[root@localhost ~]# echo $C
/media/ftp/Payam test .mp3
[root@localhost ~]# cp $C .
cp: cannot stat `/media/ftp/Payam': No such file or directory
cp: cannot stat `test': No such file or directory
cp: cannot stat `.mp3': No such file or directory
[root@localhost ~]#

now how can i solve my problem?? i used some method like:

[root@localhost ~]# C=$(grep -wi "MP3" /tmp/sync | awk -F: '{print $1}'|head -n3|tail -n1|sed 's/ /\\ /g')
[root@localhost ~]# echo $C
/media/ftp/Payam\ test\ .mp3
[root@localhost ~]# cp $C .
cp: cannot stat `/media/ftp/Payam\\': Invalid argument
cp: cannot stat `test\\': No such file or directory
cp: cannot stat `.mp3': No such file or directory
[root@localhost ~]#

but nothing happened good.
please help me solve this problem.

tanx.

Last edited by mirzasoltan; 09-29-2012 at 12:24 PM.
 
Old 09-29-2012, 12:48 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Place the variable in double quotes ("") so it will retain the white space.

Also, when displaying code, please use [code][/code] tags so it is easily readable.

Last edited by grail; 09-30-2012 at 10:35 AM.
 
Old 09-30-2012, 10:19 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It's vital in scripting to understand how the shell handles arguments and whitespace. See here:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

As grail said, always quote your variables, unless you know what you're doing.


Also, never try to insert syntactical quotes or backslashes into the variable. It won't work. Due to the order in which the shell parses the line, once a character is stored in a variable, it loses all special meaning, with the exception of whitespace* and globbing patterns (as explained above).


(*Or more accurately, the characters currently set in the IFS shell variable, space-tab-newline by default.)


Edit: BTW; your text extraction command can certainly be greatly simplified. awk is a full text processing language and can do everything that grep/head/tail can do all on its own.

Unfortunately though, without knowing exactly what /tmp/sync contains or what you want from it, I can't give you a complete solution, so I have to leave the grep in for now:

Code:
#C=$(grep -wi "MP3" /tmp/sync | awk -F: '{print $1}'|head -n3|tail -n1)

C=(grep -wi 'MP3' /tmp/sync | awk -F : 'NR=3 { print $1 }' )
Show us the file and what you want, and we can reduce it to awk alone.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Last edited by David the H.; 09-30-2012 at 10:34 AM. Reason: as stated
 
1 members found this post helpful.
  


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
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 09:36 AM
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 1 07-13-2012 01:03 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM

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

All times are GMT -5. The time now is 07:10 PM.

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