Change the shape that shows a for loop












0















I have the following code:



#!/bin/bash

FSTAB=` grep -vE "^#|swap|UUID" /etc/fstab | awk '{print $1,$2,$3}'`

for i in $FSTAB
do

echo "$i"

done


return this:



/dev/mapper/centos-root
/
xfs
/dev/sdb2
/hdos
xfs


The problem is that later I want to compare it, so I am returning it with line breaks and I want it to be returned in a single line without jumps, that is to say:



/dev/mapper/centos-root / xfs
/dev/sdb2 /hdos xfs









share|improve this question

























  • for [...]; done | tr -d 'n'.

    – DopeGhoti
    Jan 30 at 21:23











  • You'd better let us know what you are compare it to later (or even better, what the overall issue is that you're trying to solve). It may not be needed to output anything at all. In fact, the safest way to store the pathnames for later use would be in an array. That would enable you to handle pathnames with whitespaces and other funky names.

    – Kusalananda
    Jan 30 at 21:26








  • 1





    Or awk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab.

    – DopeGhoti
    Jan 30 at 21:33








  • 1





    Or letting awk write to stdout (:

    – DopeGhoti
    Jan 30 at 21:34






  • 1





    My life got a lot easier (and involved a lot fewer pipe fittings) the day I realized that grepping into awk is a kissing cousin to the Useless Use of Cat.

    – DopeGhoti
    Jan 30 at 21:42
















0















I have the following code:



#!/bin/bash

FSTAB=` grep -vE "^#|swap|UUID" /etc/fstab | awk '{print $1,$2,$3}'`

for i in $FSTAB
do

echo "$i"

done


return this:



/dev/mapper/centos-root
/
xfs
/dev/sdb2
/hdos
xfs


The problem is that later I want to compare it, so I am returning it with line breaks and I want it to be returned in a single line without jumps, that is to say:



/dev/mapper/centos-root / xfs
/dev/sdb2 /hdos xfs









share|improve this question

























  • for [...]; done | tr -d 'n'.

    – DopeGhoti
    Jan 30 at 21:23











  • You'd better let us know what you are compare it to later (or even better, what the overall issue is that you're trying to solve). It may not be needed to output anything at all. In fact, the safest way to store the pathnames for later use would be in an array. That would enable you to handle pathnames with whitespaces and other funky names.

    – Kusalananda
    Jan 30 at 21:26








  • 1





    Or awk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab.

    – DopeGhoti
    Jan 30 at 21:33








  • 1





    Or letting awk write to stdout (:

    – DopeGhoti
    Jan 30 at 21:34






  • 1





    My life got a lot easier (and involved a lot fewer pipe fittings) the day I realized that grepping into awk is a kissing cousin to the Useless Use of Cat.

    – DopeGhoti
    Jan 30 at 21:42














0












0








0








I have the following code:



#!/bin/bash

FSTAB=` grep -vE "^#|swap|UUID" /etc/fstab | awk '{print $1,$2,$3}'`

for i in $FSTAB
do

echo "$i"

done


return this:



/dev/mapper/centos-root
/
xfs
/dev/sdb2
/hdos
xfs


The problem is that later I want to compare it, so I am returning it with line breaks and I want it to be returned in a single line without jumps, that is to say:



/dev/mapper/centos-root / xfs
/dev/sdb2 /hdos xfs









share|improve this question
















I have the following code:



#!/bin/bash

FSTAB=` grep -vE "^#|swap|UUID" /etc/fstab | awk '{print $1,$2,$3}'`

for i in $FSTAB
do

echo "$i"

done


return this:



/dev/mapper/centos-root
/
xfs
/dev/sdb2
/hdos
xfs


The problem is that later I want to compare it, so I am returning it with line breaks and I want it to be returned in a single line without jumps, that is to say:



/dev/mapper/centos-root / xfs
/dev/sdb2 /hdos xfs






bash shell-script shell scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 30 at 21:51







ortiga

















asked Jan 30 at 21:17









ortigaortiga

83




83













  • for [...]; done | tr -d 'n'.

    – DopeGhoti
    Jan 30 at 21:23











  • You'd better let us know what you are compare it to later (or even better, what the overall issue is that you're trying to solve). It may not be needed to output anything at all. In fact, the safest way to store the pathnames for later use would be in an array. That would enable you to handle pathnames with whitespaces and other funky names.

    – Kusalananda
    Jan 30 at 21:26








  • 1





    Or awk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab.

    – DopeGhoti
    Jan 30 at 21:33








  • 1





    Or letting awk write to stdout (:

    – DopeGhoti
    Jan 30 at 21:34






  • 1





    My life got a lot easier (and involved a lot fewer pipe fittings) the day I realized that grepping into awk is a kissing cousin to the Useless Use of Cat.

    – DopeGhoti
    Jan 30 at 21:42



















  • for [...]; done | tr -d 'n'.

    – DopeGhoti
    Jan 30 at 21:23











  • You'd better let us know what you are compare it to later (or even better, what the overall issue is that you're trying to solve). It may not be needed to output anything at all. In fact, the safest way to store the pathnames for later use would be in an array. That would enable you to handle pathnames with whitespaces and other funky names.

    – Kusalananda
    Jan 30 at 21:26








  • 1





    Or awk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab.

    – DopeGhoti
    Jan 30 at 21:33








  • 1





    Or letting awk write to stdout (:

    – DopeGhoti
    Jan 30 at 21:34






  • 1





    My life got a lot easier (and involved a lot fewer pipe fittings) the day I realized that grepping into awk is a kissing cousin to the Useless Use of Cat.

    – DopeGhoti
    Jan 30 at 21:42

















for [...]; done | tr -d 'n'.

– DopeGhoti
Jan 30 at 21:23





for [...]; done | tr -d 'n'.

– DopeGhoti
Jan 30 at 21:23













You'd better let us know what you are compare it to later (or even better, what the overall issue is that you're trying to solve). It may not be needed to output anything at all. In fact, the safest way to store the pathnames for later use would be in an array. That would enable you to handle pathnames with whitespaces and other funky names.

– Kusalananda
Jan 30 at 21:26







You'd better let us know what you are compare it to later (or even better, what the overall issue is that you're trying to solve). It may not be needed to output anything at all. In fact, the safest way to store the pathnames for later use would be in an array. That would enable you to handle pathnames with whitespaces and other funky names.

– Kusalananda
Jan 30 at 21:26






1




1





Or awk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab.

– DopeGhoti
Jan 30 at 21:33







Or awk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab.

– DopeGhoti
Jan 30 at 21:33






1




1





Or letting awk write to stdout (:

– DopeGhoti
Jan 30 at 21:34





Or letting awk write to stdout (:

– DopeGhoti
Jan 30 at 21:34




1




1





My life got a lot easier (and involved a lot fewer pipe fittings) the day I realized that grepping into awk is a kissing cousin to the Useless Use of Cat.

– DopeGhoti
Jan 30 at 21:42





My life got a lot easier (and involved a lot fewer pipe fittings) the day I realized that grepping into awk is a kissing cousin to the Useless Use of Cat.

– DopeGhoti
Jan 30 at 21:42










3 Answers
3






active

oldest

votes


















2














Regardless of how you fill the FSTAB variable, the unquoted expansion of it in the for loop removes the distinction between different kinds of white space. (See: When is double-quoting necessary? and Why does my shell script choke on whitespace or other special characters?)



To work around that, either set IFS to split only on newlines, or loop over the lines with a while read loop:



$ fstab=$(grep /etc/fstab ...)
$ while IFS= read -r line; do
printf "$linen"
done <<< "$fstab"


You could also use while read -r dev mnt fs; do ... if you wanted the fields in separate variables.






share|improve this answer































    2














    Your entire variable assignment command chain there can be done in one awk rather than piping grep's output into awk:



    FSTAB=` grep -v "^#" /etc/fstab | grep . | grep -v "swap" | grep -v "UUID"   | awk '{print $1,$2,$3}'`


    can instead be



    FSTAB="$(awk 'NF && $0 !~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab)"


    Newlines will be embedded in the variable, so you don't need to loop anything, you can simply echo "$FSTAB".



    However, rather than parsing /etc/fstab into a variable and then parsing that, it would probably be better to parse /etc/fstab directly with a more involved awk script that does exactly what you need to with the output you're currently scraping from it.






    share|improve this answer

































      0














      Just GNU egrep:



      egrep -v "^#|swap|UUID" /etc/fstab  | egrep -o '^(S*s*){1,3}'





      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "106"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497785%2fchange-the-shape-that-shows-a-for-loop%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        Regardless of how you fill the FSTAB variable, the unquoted expansion of it in the for loop removes the distinction between different kinds of white space. (See: When is double-quoting necessary? and Why does my shell script choke on whitespace or other special characters?)



        To work around that, either set IFS to split only on newlines, or loop over the lines with a while read loop:



        $ fstab=$(grep /etc/fstab ...)
        $ while IFS= read -r line; do
        printf "$linen"
        done <<< "$fstab"


        You could also use while read -r dev mnt fs; do ... if you wanted the fields in separate variables.






        share|improve this answer




























          2














          Regardless of how you fill the FSTAB variable, the unquoted expansion of it in the for loop removes the distinction between different kinds of white space. (See: When is double-quoting necessary? and Why does my shell script choke on whitespace or other special characters?)



          To work around that, either set IFS to split only on newlines, or loop over the lines with a while read loop:



          $ fstab=$(grep /etc/fstab ...)
          $ while IFS= read -r line; do
          printf "$linen"
          done <<< "$fstab"


          You could also use while read -r dev mnt fs; do ... if you wanted the fields in separate variables.






          share|improve this answer


























            2












            2








            2







            Regardless of how you fill the FSTAB variable, the unquoted expansion of it in the for loop removes the distinction between different kinds of white space. (See: When is double-quoting necessary? and Why does my shell script choke on whitespace or other special characters?)



            To work around that, either set IFS to split only on newlines, or loop over the lines with a while read loop:



            $ fstab=$(grep /etc/fstab ...)
            $ while IFS= read -r line; do
            printf "$linen"
            done <<< "$fstab"


            You could also use while read -r dev mnt fs; do ... if you wanted the fields in separate variables.






            share|improve this answer













            Regardless of how you fill the FSTAB variable, the unquoted expansion of it in the for loop removes the distinction between different kinds of white space. (See: When is double-quoting necessary? and Why does my shell script choke on whitespace or other special characters?)



            To work around that, either set IFS to split only on newlines, or loop over the lines with a while read loop:



            $ fstab=$(grep /etc/fstab ...)
            $ while IFS= read -r line; do
            printf "$linen"
            done <<< "$fstab"


            You could also use while read -r dev mnt fs; do ... if you wanted the fields in separate variables.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 30 at 22:06









            ilkkachuilkkachu

            58.3k890164




            58.3k890164

























                2














                Your entire variable assignment command chain there can be done in one awk rather than piping grep's output into awk:



                FSTAB=` grep -v "^#" /etc/fstab | grep . | grep -v "swap" | grep -v "UUID"   | awk '{print $1,$2,$3}'`


                can instead be



                FSTAB="$(awk 'NF && $0 !~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab)"


                Newlines will be embedded in the variable, so you don't need to loop anything, you can simply echo "$FSTAB".



                However, rather than parsing /etc/fstab into a variable and then parsing that, it would probably be better to parse /etc/fstab directly with a more involved awk script that does exactly what you need to with the output you're currently scraping from it.






                share|improve this answer






























                  2














                  Your entire variable assignment command chain there can be done in one awk rather than piping grep's output into awk:



                  FSTAB=` grep -v "^#" /etc/fstab | grep . | grep -v "swap" | grep -v "UUID"   | awk '{print $1,$2,$3}'`


                  can instead be



                  FSTAB="$(awk 'NF && $0 !~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab)"


                  Newlines will be embedded in the variable, so you don't need to loop anything, you can simply echo "$FSTAB".



                  However, rather than parsing /etc/fstab into a variable and then parsing that, it would probably be better to parse /etc/fstab directly with a more involved awk script that does exactly what you need to with the output you're currently scraping from it.






                  share|improve this answer




























                    2












                    2








                    2







                    Your entire variable assignment command chain there can be done in one awk rather than piping grep's output into awk:



                    FSTAB=` grep -v "^#" /etc/fstab | grep . | grep -v "swap" | grep -v "UUID"   | awk '{print $1,$2,$3}'`


                    can instead be



                    FSTAB="$(awk 'NF && $0 !~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab)"


                    Newlines will be embedded in the variable, so you don't need to loop anything, you can simply echo "$FSTAB".



                    However, rather than parsing /etc/fstab into a variable and then parsing that, it would probably be better to parse /etc/fstab directly with a more involved awk script that does exactly what you need to with the output you're currently scraping from it.






                    share|improve this answer















                    Your entire variable assignment command chain there can be done in one awk rather than piping grep's output into awk:



                    FSTAB=` grep -v "^#" /etc/fstab | grep . | grep -v "swap" | grep -v "UUID"   | awk '{print $1,$2,$3}'`


                    can instead be



                    FSTAB="$(awk 'NF && $0 !~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab)"


                    Newlines will be embedded in the variable, so you don't need to loop anything, you can simply echo "$FSTAB".



                    However, rather than parsing /etc/fstab into a variable and then parsing that, it would probably be better to parse /etc/fstab directly with a more involved awk script that does exactly what you need to with the output you're currently scraping from it.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 30 at 22:17

























                    answered Jan 30 at 21:40









                    DopeGhotiDopeGhoti

                    45.4k55988




                    45.4k55988























                        0














                        Just GNU egrep:



                        egrep -v "^#|swap|UUID" /etc/fstab  | egrep -o '^(S*s*){1,3}'





                        share|improve this answer




























                          0














                          Just GNU egrep:



                          egrep -v "^#|swap|UUID" /etc/fstab  | egrep -o '^(S*s*){1,3}'





                          share|improve this answer


























                            0












                            0








                            0







                            Just GNU egrep:



                            egrep -v "^#|swap|UUID" /etc/fstab  | egrep -o '^(S*s*){1,3}'





                            share|improve this answer













                            Just GNU egrep:



                            egrep -v "^#|swap|UUID" /etc/fstab  | egrep -o '^(S*s*){1,3}'






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 4 at 4:32









                            agcagc

                            4,67111137




                            4,67111137






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497785%2fchange-the-shape-that-shows-a-for-loop%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to make a Squid Proxy server?

                                Is this a new Fibonacci Identity?

                                19世紀