How to convert a very small bash script into a single line command
I have a following bash script and it is being executed using popen(/path/to/script, 'r+') from C code and saving result back (Ex: DA00000);
for this, i need to export a bash script file.
But my requirement is that i should not use a seperate bash script.
Can you please let me know how to write below script in a single line itself as command?
So, i can use this command directly ( Ex:fp = popen(command "r")) in C code.
#!/bin/sh
url="$(grep 0x017a /sys/bus/pci/devices/*/device)"
addr="$(echo $url | cut -d/ -f6)"
str="$(head -n1 /sys/bus/pci/devices/$addr/resource | cut -d ' ' -f 1)"
result="${str:${#str} - 8}"
echo $result
shell-script
add a comment |
I have a following bash script and it is being executed using popen(/path/to/script, 'r+') from C code and saving result back (Ex: DA00000);
for this, i need to export a bash script file.
But my requirement is that i should not use a seperate bash script.
Can you please let me know how to write below script in a single line itself as command?
So, i can use this command directly ( Ex:fp = popen(command "r")) in C code.
#!/bin/sh
url="$(grep 0x017a /sys/bus/pci/devices/*/device)"
addr="$(echo $url | cut -d/ -f6)"
str="$(head -n1 /sys/bus/pci/devices/$addr/resource | cut -d ' ' -f 1)"
result="${str:${#str} - 8}"
echo $result
shell-script
1
I would suggest that you, instead of executing an external shell script, opened thedevice
files directly and read from them with your C program.
– Kusalananda
Feb 11 at 10:04
But i have to read all devices to find exact match i.e. 0x017a since there are many device file are available.
– Seshagiri Lekkala
Feb 11 at 10:10
notice that your script is broken -- the/sys/bus/pice/devices/*/device
files do not contain any url or file name; you probably wantgrep -l
instead.
– mosvy
Feb 11 at 10:30
add a comment |
I have a following bash script and it is being executed using popen(/path/to/script, 'r+') from C code and saving result back (Ex: DA00000);
for this, i need to export a bash script file.
But my requirement is that i should not use a seperate bash script.
Can you please let me know how to write below script in a single line itself as command?
So, i can use this command directly ( Ex:fp = popen(command "r")) in C code.
#!/bin/sh
url="$(grep 0x017a /sys/bus/pci/devices/*/device)"
addr="$(echo $url | cut -d/ -f6)"
str="$(head -n1 /sys/bus/pci/devices/$addr/resource | cut -d ' ' -f 1)"
result="${str:${#str} - 8}"
echo $result
shell-script
I have a following bash script and it is being executed using popen(/path/to/script, 'r+') from C code and saving result back (Ex: DA00000);
for this, i need to export a bash script file.
But my requirement is that i should not use a seperate bash script.
Can you please let me know how to write below script in a single line itself as command?
So, i can use this command directly ( Ex:fp = popen(command "r")) in C code.
#!/bin/sh
url="$(grep 0x017a /sys/bus/pci/devices/*/device)"
addr="$(echo $url | cut -d/ -f6)"
str="$(head -n1 /sys/bus/pci/devices/$addr/resource | cut -d ' ' -f 1)"
result="${str:${#str} - 8}"
echo $result
shell-script
shell-script
edited Feb 11 at 10:02
Seshagiri Lekkala
asked Feb 11 at 9:54
Seshagiri LekkalaSeshagiri Lekkala
154
154
1
I would suggest that you, instead of executing an external shell script, opened thedevice
files directly and read from them with your C program.
– Kusalananda
Feb 11 at 10:04
But i have to read all devices to find exact match i.e. 0x017a since there are many device file are available.
– Seshagiri Lekkala
Feb 11 at 10:10
notice that your script is broken -- the/sys/bus/pice/devices/*/device
files do not contain any url or file name; you probably wantgrep -l
instead.
– mosvy
Feb 11 at 10:30
add a comment |
1
I would suggest that you, instead of executing an external shell script, opened thedevice
files directly and read from them with your C program.
– Kusalananda
Feb 11 at 10:04
But i have to read all devices to find exact match i.e. 0x017a since there are many device file are available.
– Seshagiri Lekkala
Feb 11 at 10:10
notice that your script is broken -- the/sys/bus/pice/devices/*/device
files do not contain any url or file name; you probably wantgrep -l
instead.
– mosvy
Feb 11 at 10:30
1
1
I would suggest that you, instead of executing an external shell script, opened the
device
files directly and read from them with your C program.– Kusalananda
Feb 11 at 10:04
I would suggest that you, instead of executing an external shell script, opened the
device
files directly and read from them with your C program.– Kusalananda
Feb 11 at 10:04
But i have to read all devices to find exact match i.e. 0x017a since there are many device file are available.
– Seshagiri Lekkala
Feb 11 at 10:10
But i have to read all devices to find exact match i.e. 0x017a since there are many device file are available.
– Seshagiri Lekkala
Feb 11 at 10:10
notice that your script is broken -- the
/sys/bus/pice/devices/*/device
files do not contain any url or file name; you probably want grep -l
instead.– mosvy
Feb 11 at 10:30
notice that your script is broken -- the
/sys/bus/pice/devices/*/device
files do not contain any url or file name; you probably want grep -l
instead.– mosvy
Feb 11 at 10:30
add a comment |
2 Answers
2
active
oldest
votes
You can convert your script to a C string and then pass that string directly to the popen(3)
function.
Notice that popen(3)
takes a script which will be passed as an argument to sh -c
(and which can contain any valid shell commands) not just a program name or a command line.
Example:
$ perl -e '
print qq{char *script =n};
while(<>){
s/([-x1f"\x7f-xff])/sprintf "\x%02x",ord$1/ge;
print qq{t"$_"n}
}
print qq{;n}
' script.sh > script.h
$ cat script.h
char *script =
...
Then in your C
file:
#include script.h
...
FILE *fh = popen(script, "r");
May i know how to convert into a C string without using Perl? because i may not be able to use perl also
– Seshagiri Lekkala
Feb 11 at 10:29
You can do it inC
;-). I think that there are already programs likebin2c
or such, that perl thing was a quick and dirty example.
– mosvy
Feb 11 at 10:31
but you would probably be better off by just doing the whole thing inC
; that script is defective and overly complicated, anyway.
– mosvy
Feb 11 at 10:35
add a comment |
I come up with two pass:
first part (url+addr)
awk -F/ '/0x017a/ {print $6}' /sys/bus/pci/devices/*/device
second part
awk 'NR==1 { print substr($1,1,length($1)-8;}' /sys/bus/pci/devices/$addr/resource
I am not quiet sure this is a good way to address the problem.
I executed above command but didnt work as expected.
– Seshagiri Lekkala
Feb 11 at 10:31
Your comment doesn't help me as expected.
– Archemar
Feb 11 at 10:33
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%2f499901%2fhow-to-convert-a-very-small-bash-script-into-a-single-line-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can convert your script to a C string and then pass that string directly to the popen(3)
function.
Notice that popen(3)
takes a script which will be passed as an argument to sh -c
(and which can contain any valid shell commands) not just a program name or a command line.
Example:
$ perl -e '
print qq{char *script =n};
while(<>){
s/([-x1f"\x7f-xff])/sprintf "\x%02x",ord$1/ge;
print qq{t"$_"n}
}
print qq{;n}
' script.sh > script.h
$ cat script.h
char *script =
...
Then in your C
file:
#include script.h
...
FILE *fh = popen(script, "r");
May i know how to convert into a C string without using Perl? because i may not be able to use perl also
– Seshagiri Lekkala
Feb 11 at 10:29
You can do it inC
;-). I think that there are already programs likebin2c
or such, that perl thing was a quick and dirty example.
– mosvy
Feb 11 at 10:31
but you would probably be better off by just doing the whole thing inC
; that script is defective and overly complicated, anyway.
– mosvy
Feb 11 at 10:35
add a comment |
You can convert your script to a C string and then pass that string directly to the popen(3)
function.
Notice that popen(3)
takes a script which will be passed as an argument to sh -c
(and which can contain any valid shell commands) not just a program name or a command line.
Example:
$ perl -e '
print qq{char *script =n};
while(<>){
s/([-x1f"\x7f-xff])/sprintf "\x%02x",ord$1/ge;
print qq{t"$_"n}
}
print qq{;n}
' script.sh > script.h
$ cat script.h
char *script =
...
Then in your C
file:
#include script.h
...
FILE *fh = popen(script, "r");
May i know how to convert into a C string without using Perl? because i may not be able to use perl also
– Seshagiri Lekkala
Feb 11 at 10:29
You can do it inC
;-). I think that there are already programs likebin2c
or such, that perl thing was a quick and dirty example.
– mosvy
Feb 11 at 10:31
but you would probably be better off by just doing the whole thing inC
; that script is defective and overly complicated, anyway.
– mosvy
Feb 11 at 10:35
add a comment |
You can convert your script to a C string and then pass that string directly to the popen(3)
function.
Notice that popen(3)
takes a script which will be passed as an argument to sh -c
(and which can contain any valid shell commands) not just a program name or a command line.
Example:
$ perl -e '
print qq{char *script =n};
while(<>){
s/([-x1f"\x7f-xff])/sprintf "\x%02x",ord$1/ge;
print qq{t"$_"n}
}
print qq{;n}
' script.sh > script.h
$ cat script.h
char *script =
...
Then in your C
file:
#include script.h
...
FILE *fh = popen(script, "r");
You can convert your script to a C string and then pass that string directly to the popen(3)
function.
Notice that popen(3)
takes a script which will be passed as an argument to sh -c
(and which can contain any valid shell commands) not just a program name or a command line.
Example:
$ perl -e '
print qq{char *script =n};
while(<>){
s/([-x1f"\x7f-xff])/sprintf "\x%02x",ord$1/ge;
print qq{t"$_"n}
}
print qq{;n}
' script.sh > script.h
$ cat script.h
char *script =
...
Then in your C
file:
#include script.h
...
FILE *fh = popen(script, "r");
edited Feb 11 at 11:00
answered Feb 11 at 10:15
mosvymosvy
7,7321530
7,7321530
May i know how to convert into a C string without using Perl? because i may not be able to use perl also
– Seshagiri Lekkala
Feb 11 at 10:29
You can do it inC
;-). I think that there are already programs likebin2c
or such, that perl thing was a quick and dirty example.
– mosvy
Feb 11 at 10:31
but you would probably be better off by just doing the whole thing inC
; that script is defective and overly complicated, anyway.
– mosvy
Feb 11 at 10:35
add a comment |
May i know how to convert into a C string without using Perl? because i may not be able to use perl also
– Seshagiri Lekkala
Feb 11 at 10:29
You can do it inC
;-). I think that there are already programs likebin2c
or such, that perl thing was a quick and dirty example.
– mosvy
Feb 11 at 10:31
but you would probably be better off by just doing the whole thing inC
; that script is defective and overly complicated, anyway.
– mosvy
Feb 11 at 10:35
May i know how to convert into a C string without using Perl? because i may not be able to use perl also
– Seshagiri Lekkala
Feb 11 at 10:29
May i know how to convert into a C string without using Perl? because i may not be able to use perl also
– Seshagiri Lekkala
Feb 11 at 10:29
You can do it in
C
;-). I think that there are already programs like bin2c
or such, that perl thing was a quick and dirty example.– mosvy
Feb 11 at 10:31
You can do it in
C
;-). I think that there are already programs like bin2c
or such, that perl thing was a quick and dirty example.– mosvy
Feb 11 at 10:31
but you would probably be better off by just doing the whole thing in
C
; that script is defective and overly complicated, anyway.– mosvy
Feb 11 at 10:35
but you would probably be better off by just doing the whole thing in
C
; that script is defective and overly complicated, anyway.– mosvy
Feb 11 at 10:35
add a comment |
I come up with two pass:
first part (url+addr)
awk -F/ '/0x017a/ {print $6}' /sys/bus/pci/devices/*/device
second part
awk 'NR==1 { print substr($1,1,length($1)-8;}' /sys/bus/pci/devices/$addr/resource
I am not quiet sure this is a good way to address the problem.
I executed above command but didnt work as expected.
– Seshagiri Lekkala
Feb 11 at 10:31
Your comment doesn't help me as expected.
– Archemar
Feb 11 at 10:33
add a comment |
I come up with two pass:
first part (url+addr)
awk -F/ '/0x017a/ {print $6}' /sys/bus/pci/devices/*/device
second part
awk 'NR==1 { print substr($1,1,length($1)-8;}' /sys/bus/pci/devices/$addr/resource
I am not quiet sure this is a good way to address the problem.
I executed above command but didnt work as expected.
– Seshagiri Lekkala
Feb 11 at 10:31
Your comment doesn't help me as expected.
– Archemar
Feb 11 at 10:33
add a comment |
I come up with two pass:
first part (url+addr)
awk -F/ '/0x017a/ {print $6}' /sys/bus/pci/devices/*/device
second part
awk 'NR==1 { print substr($1,1,length($1)-8;}' /sys/bus/pci/devices/$addr/resource
I am not quiet sure this is a good way to address the problem.
I come up with two pass:
first part (url+addr)
awk -F/ '/0x017a/ {print $6}' /sys/bus/pci/devices/*/device
second part
awk 'NR==1 { print substr($1,1,length($1)-8;}' /sys/bus/pci/devices/$addr/resource
I am not quiet sure this is a good way to address the problem.
answered Feb 11 at 10:19
ArchemarArchemar
20.2k93772
20.2k93772
I executed above command but didnt work as expected.
– Seshagiri Lekkala
Feb 11 at 10:31
Your comment doesn't help me as expected.
– Archemar
Feb 11 at 10:33
add a comment |
I executed above command but didnt work as expected.
– Seshagiri Lekkala
Feb 11 at 10:31
Your comment doesn't help me as expected.
– Archemar
Feb 11 at 10:33
I executed above command but didnt work as expected.
– Seshagiri Lekkala
Feb 11 at 10:31
I executed above command but didnt work as expected.
– Seshagiri Lekkala
Feb 11 at 10:31
Your comment doesn't help me as expected.
– Archemar
Feb 11 at 10:33
Your comment doesn't help me as expected.
– Archemar
Feb 11 at 10:33
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%2f499901%2fhow-to-convert-a-very-small-bash-script-into-a-single-line-command%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
1
I would suggest that you, instead of executing an external shell script, opened the
device
files directly and read from them with your C program.– Kusalananda
Feb 11 at 10:04
But i have to read all devices to find exact match i.e. 0x017a since there are many device file are available.
– Seshagiri Lekkala
Feb 11 at 10:10
notice that your script is broken -- the
/sys/bus/pice/devices/*/device
files do not contain any url or file name; you probably wantgrep -l
instead.– mosvy
Feb 11 at 10:30