LinuxQuestions.org
Review your favorite Linux distribution.
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 06-27-2014, 04:25 PM   #1
Jeff9
Member
 
Registered: Jun 2013
Posts: 36

Rep: Reputation: Disabled
Quotes v Apostrophes


Linux (as you all know) does not tell you what command it is executing. So when I write a loop to do a lot of file processing, I write it like the following. The issue here is the echo command followed by the exact same command.

Code:
for ((TT=10000; TT<100000; TT+=10000))
do
        for fn in N\=00[06][48]*Beta=0[456789]*forward.U.csv N\=00[06][48]*Beta=1[012]*forward.U.csv N\=0032*Beta=0[45678]*forward.U.csv N\=1024*Beta=0[45678]*forward.U.csv;
        do

# Arbitrary string manipulation to create new file name $nfn

                echo "head -n '${TT}' '$fn' | tail -n 1 >../'$nfn'"
                      head -n "${TT}" "$fn" | tail -n 1 >../"$nfn"
        done
done
I would be happy with this solution -- I really should be happy with it as far as I got it, but I can't -- if I didn't have to change the quotes to apostrophes on the second line. That is, first I write the echo line and test (and repeat). When I (eventually!) get the echo line correct, I copy it, paste it, and delete the letters characters e, c, h, o, and quotes ("). If that's all I had to do, I'd be satisfied. But now I have to change the remaining apostrophes (') to quotes ("). How annoying!

Am I using quotes and apostrophes correctly? I tried switching them in the first line, so that all I would have to do in the pasted line was remove the e, c, h, o, and apostrophes. But that had disastrous results.
 
Old 06-27-2014, 05:02 PM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
How about:
Code:
command="head -n \"${TT}\" \"$fn\" | tail -n 1 >../\"$nfn\""
echo $command
$command
p.s. - I didn't look at what you are trying to do with this command, only your question about quotes.
 
1 members found this post helpful.
Old 06-27-2014, 05:33 PM   #3
Jeff9
Member
 
Registered: Jun 2013
Posts: 36

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by haertig View Post
p.s. - I didn't look at what you are trying to do with this command, only your question about quotes.
I realized that very quickly. . But the idea is that you can execute a string (which was news to me). So just use escape characters to make the string. Seems simple enough.

I tried your idea and a few variations on it. But I'm still not quite getting it to work because Linux keeps insisting on sending the output to the screen instead of the output file that I want. I get the message that "head cannot open <the output file>". for <the output file>, it's listing value of $nfn.

Code:
# Old way of doin' it.
#    echo "head -n 1 '${fn}' >../'${fn/_tol=1e-08_t=00000_forward.U.csv/_t=00000_forward.IC.csv}'"
#          head -n 1 "${fn}" >../"${fn/_tol=1e-08_t=00000_forward.U.csv/_t=00000_forward.IC.csv}"

        nfn=${fn/_tol=1e-08_t=00000_forward.U.csv/_t=00000_forward.IC.csv}
        cmdString="head -n 1 \"${fn}\" >../\"${nfn}\""

        echo $cmdString
        $cmdString

Last edited by Jeff9; 06-27-2014 at 05:35 PM.
 
Old 06-27-2014, 06:11 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,124

Rep: Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346Reputation: 1346
Quote:
Originally Posted by Jeff9 View Post
Linux (as you all know) does not tell you what command it is executing. So when I write a loop to do a lot of file processing, I write it like the following. The issue here is the echo command followed by the exact same command.
What I do is to define a function that I call "run" that displays the arguments it was called with and then executes that as a command:
Code:
run() {          # Display a command and run it
    [ $Verbose = y ] && echo "${PS4}$*" >&2
    PS4="+$PS4" "$@"
}
run head -n "${TT}" "$fn" | tail -n 1 >../"$nfn"
The display is under the control of variable "$Verbose", but that is easily changed. The use of "$@" preserves the word splitting of the original arguments without having to play with quoting. The use of and manipulation of PS4 makes the output imitate what the shell's "-x" option would produce at each shell level. The return code from the executed command is preserved as the return code from the run() function.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 04:03 PM
[SOLVED] bash substitution on apostrophes help otkaz Programming 9 10-06-2011 11:44 AM
Why does man display apostrophes incorrectly? syssyphus Linux - General 1 10-01-2005 07:32 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM
My first alias in .bashrc...'apostrophes 'getting' in the way?' Baix Linux - Newbie 3 06-17-2004 03:36 PM


All times are GMT -5. The time now is 02:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration