Change the shape that shows a for loop
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
|
show 5 more comments
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
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
Orawk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab
.
– DopeGhoti
Jan 30 at 21:33
1
Or lettingawk
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 thatgrep
ping intoawk
is a kissing cousin to the Useless Use of Cat.
– DopeGhoti
Jan 30 at 21:42
|
show 5 more comments
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
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
bash shell-script shell scripting
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
Orawk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab
.
– DopeGhoti
Jan 30 at 21:33
1
Or lettingawk
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 thatgrep
ping intoawk
is a kissing cousin to the Useless Use of Cat.
– DopeGhoti
Jan 30 at 21:42
|
show 5 more comments
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
Orawk '$0 ! ~ /^#|swap|UUID/ {print $1, $2, $3}' /etc/fstab
.
– DopeGhoti
Jan 30 at 21:33
1
Or lettingawk
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 thatgrep
ping intoawk
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
grep
ping 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
grep
ping into awk
is a kissing cousin to the Useless Use of Cat.– DopeGhoti
Jan 30 at 21:42
|
show 5 more comments
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
Just GNU egrep
:
egrep -v "^#|swap|UUID" /etc/fstab | egrep -o '^(S*s*){1,3}'
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 30 at 22:06
ilkkachuilkkachu
58.3k890164
58.3k890164
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 30 at 22:17
answered Jan 30 at 21:40
DopeGhotiDopeGhoti
45.4k55988
45.4k55988
add a comment |
add a comment |
Just GNU egrep
:
egrep -v "^#|swap|UUID" /etc/fstab | egrep -o '^(S*s*){1,3}'
add a comment |
Just GNU egrep
:
egrep -v "^#|swap|UUID" /etc/fstab | egrep -o '^(S*s*){1,3}'
add a comment |
Just GNU egrep
:
egrep -v "^#|swap|UUID" /etc/fstab | egrep -o '^(S*s*){1,3}'
Just GNU egrep
:
egrep -v "^#|swap|UUID" /etc/fstab | egrep -o '^(S*s*){1,3}'
answered Feb 4 at 4:32
agcagc
4,67111137
4,67111137
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
grep
ping intoawk
is a kissing cousin to the Useless Use of Cat.– DopeGhoti
Jan 30 at 21:42