Command not working properly in shell script
Here is output of:
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While same thing written in script is not working same.
Here is a function in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name";
done
}
I got output like this :
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: when there is a space in putput it take as a new line.
I'm working on ubuntu 18.04.
shell-script
add a comment |
Here is output of:
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While same thing written in script is not working same.
Here is a function in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name";
done
}
I got output like this :
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: when there is a space in putput it take as a new line.
I'm working on ubuntu 18.04.
shell-script
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
2 hours ago
i didn't find that (might be due to word match) so I created question
– yash shah
2 hours ago
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
2 hours ago
yaa... it's really useful link
– yash shah
2 hours ago
add a comment |
Here is output of:
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While same thing written in script is not working same.
Here is a function in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name";
done
}
I got output like this :
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: when there is a space in putput it take as a new line.
I'm working on ubuntu 18.04.
shell-script
Here is output of:
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null
Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G
While same thing written in script is not working same.
Here is a function in which I used above command.
for ssid_name in $(sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name";
done
}
I got output like this :
Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G
Note: when there is a space in putput it take as a new line.
I'm working on ubuntu 18.04.
shell-script
shell-script
asked 3 hours ago
yash shahyash shah
579
579
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
2 hours ago
i didn't find that (might be due to word match) so I created question
– yash shah
2 hours ago
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
2 hours ago
yaa... it's really useful link
– yash shah
2 hours ago
add a comment |
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
2 hours ago
i didn't find that (might be due to word match) so I created question
– yash shah
2 hours ago
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
2 hours ago
yaa... it's really useful link
– yash shah
2 hours ago
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
2 hours ago
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
2 hours ago
i didn't find that (might be due to word match) so I created question
– yash shah
2 hours ago
i didn't find that (might be due to word match) so I created question
– yash shah
2 hours ago
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
2 hours ago
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
2 hours ago
yaa... it's really useful link
– yash shah
2 hours ago
yaa... it's really useful link
– yash shah
2 hours ago
add a comment |
3 Answers
3
active
oldest
votes
First, note that it is not one command behaving differently. In your code snippet, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), whose default value is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2f494754%2fcommand-not-working-properly-in-shell-script%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
First, note that it is not one command behaving differently. In your code snippet, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), whose default value is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
add a comment |
First, note that it is not one command behaving differently. In your code snippet, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), whose default value is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
add a comment |
First, note that it is not one command behaving differently. In your code snippet, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), whose default value is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
First, note that it is not one command behaving differently. In your code snippet, standard output is written by two quite different commands.
Your first one prints sed
's output, while in your second one:
- The output of
sed
is substituted (by the command substitution$(...)
) afterin
; - The resulting string is expanded into a list of elements;
- Each item is in turn assigned to
ssid_name
and printed byecho
.
Expansion in point 2 is performed according to the value of the internal field separator (IFS
), whose default value is <space><tab><newline>
. Thus, your solution works because it lets for
split the substituted string on newlines only.
As an aside on your answer, note that $'n'
is not portable - even if that syntax ($'string'
) is supported in many shells.
You can nevertheless use an assignment like this:
IFS='
'
An alternative construct for processing lines of input is using read
in a while
loop:
sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done
Here, too, each line of sed
's output is split according to the value of IFS
- that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read
regardless of IFS
.
edited 32 mins ago
answered 1 hour ago
fra-sanfra-san
1,287214
1,287214
add a comment |
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
add a comment |
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
By adding following line in code before for loop, work perfactly in my case.
IFS=$'n'
answered 3 hours ago
yash shahyash shah
579
579
add a comment |
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
add a comment |
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
Better use a while
loop with read
instead of a for
loop.
sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
do
echo $ssid_name
done
see also https://mywiki.wooledge.org/BashFAQ/001
answered 2 hours ago
BodoBodo
2986
2986
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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%2f494754%2fcommand-not-working-properly-in-shell-script%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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
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
Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368
– Haxiel
2 hours ago
i didn't find that (might be due to word match) so I created question
– yash shah
2 hours ago
No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.
– Haxiel
2 hours ago
yaa... it's really useful link
– yash shah
2 hours ago