How shall I pass two groups of arguments from a shell script to two commands?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
If I need to pass command line arguments from a bash script to a command without change, here is my script:
args=("$@")
mycommand "$args[@]"
If I need to split the command line arguments from a bash script into two groups, for two commands, for example,
$ myscript -s "-o pid,tname,time,ucmd" -g "-d, -u t -t pts/4"
here is myscript
while getopts ":s:g:" opt; do
case $opt in
s)
argss="$OPTARG"
;;
g)
argsg="$OPTARG"
;;
esac
done
ps -p $(pgrep $argsg) $argss
So the call to the script is effectively the same as:
$ ps -p $(pgrep -d, -u t -t pts/4) -o pid,tname,time,ucmd
The script doesn't have double quotes around the expansions of the arguments, since it relies on the assumption that the whitespaces in $argsg
and $argss
exactly separate the arguments. As many already know, the assumption may sometimes fail to hold: an argument may contain whitespace. If there were just one command to pass arguments to, I would have used an array (at the beginning of this post). So is there a better way to wrap the two commands together?
By the way, I am trying to wrap ps
with pgrep
to allow me to AND the conditions for selecting processes. It doesn't seem necessary to write such a script (doesn't seem to simplify much), but I can save this script as a reminder when I don't remember it, and I may have other cases which makes it seem more necessary when I don't have better idea.
Thanks.
bash shell-script
add a comment |
up vote
1
down vote
favorite
If I need to pass command line arguments from a bash script to a command without change, here is my script:
args=("$@")
mycommand "$args[@]"
If I need to split the command line arguments from a bash script into two groups, for two commands, for example,
$ myscript -s "-o pid,tname,time,ucmd" -g "-d, -u t -t pts/4"
here is myscript
while getopts ":s:g:" opt; do
case $opt in
s)
argss="$OPTARG"
;;
g)
argsg="$OPTARG"
;;
esac
done
ps -p $(pgrep $argsg) $argss
So the call to the script is effectively the same as:
$ ps -p $(pgrep -d, -u t -t pts/4) -o pid,tname,time,ucmd
The script doesn't have double quotes around the expansions of the arguments, since it relies on the assumption that the whitespaces in $argsg
and $argss
exactly separate the arguments. As many already know, the assumption may sometimes fail to hold: an argument may contain whitespace. If there were just one command to pass arguments to, I would have used an array (at the beginning of this post). So is there a better way to wrap the two commands together?
By the way, I am trying to wrap ps
with pgrep
to allow me to AND the conditions for selecting processes. It doesn't seem necessary to write such a script (doesn't seem to simplify much), but I can save this script as a reminder when I don't remember it, and I may have other cases which makes it seem more necessary when I don't have better idea.
Thanks.
bash shell-script
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
If I need to pass command line arguments from a bash script to a command without change, here is my script:
args=("$@")
mycommand "$args[@]"
If I need to split the command line arguments from a bash script into two groups, for two commands, for example,
$ myscript -s "-o pid,tname,time,ucmd" -g "-d, -u t -t pts/4"
here is myscript
while getopts ":s:g:" opt; do
case $opt in
s)
argss="$OPTARG"
;;
g)
argsg="$OPTARG"
;;
esac
done
ps -p $(pgrep $argsg) $argss
So the call to the script is effectively the same as:
$ ps -p $(pgrep -d, -u t -t pts/4) -o pid,tname,time,ucmd
The script doesn't have double quotes around the expansions of the arguments, since it relies on the assumption that the whitespaces in $argsg
and $argss
exactly separate the arguments. As many already know, the assumption may sometimes fail to hold: an argument may contain whitespace. If there were just one command to pass arguments to, I would have used an array (at the beginning of this post). So is there a better way to wrap the two commands together?
By the way, I am trying to wrap ps
with pgrep
to allow me to AND the conditions for selecting processes. It doesn't seem necessary to write such a script (doesn't seem to simplify much), but I can save this script as a reminder when I don't remember it, and I may have other cases which makes it seem more necessary when I don't have better idea.
Thanks.
bash shell-script
If I need to pass command line arguments from a bash script to a command without change, here is my script:
args=("$@")
mycommand "$args[@]"
If I need to split the command line arguments from a bash script into two groups, for two commands, for example,
$ myscript -s "-o pid,tname,time,ucmd" -g "-d, -u t -t pts/4"
here is myscript
while getopts ":s:g:" opt; do
case $opt in
s)
argss="$OPTARG"
;;
g)
argsg="$OPTARG"
;;
esac
done
ps -p $(pgrep $argsg) $argss
So the call to the script is effectively the same as:
$ ps -p $(pgrep -d, -u t -t pts/4) -o pid,tname,time,ucmd
The script doesn't have double quotes around the expansions of the arguments, since it relies on the assumption that the whitespaces in $argsg
and $argss
exactly separate the arguments. As many already know, the assumption may sometimes fail to hold: an argument may contain whitespace. If there were just one command to pass arguments to, I would have used an array (at the beginning of this post). So is there a better way to wrap the two commands together?
By the way, I am trying to wrap ps
with pgrep
to allow me to AND the conditions for selecting processes. It doesn't seem necessary to write such a script (doesn't seem to simplify much), but I can save this script as a reminder when I don't remember it, and I may have other cases which makes it seem more necessary when I don't have better idea.
Thanks.
bash shell-script
bash shell-script
edited Dec 4 at 20:54
asked Dec 4 at 20:31
Tim
25.4k73243447
25.4k73243447
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Pass everyone of the arguments as an independent argument on the command line and take advantage of the option processing loop to decide to with array each argument should be added:
#!/bin/bash
for opt; do
if [[ $opt == -s ]]; then sel=s; continue; fi
if [[ $opt == -g ]]; then sel=g; continue; fi
if [[ $sel == s ]]; then argss+=("$opt");
elif [[ $sel == g ]]; then argsg+=("$opt");
else
echo "There is an error with the value of val=$val"
exit 3
fi
done
echo pgrep "$argsg[@]"
pgrep "$argsg[@]"
echo ps "$argss[@]"
ps "$argss[@]" -p "$(pgrep "$argsg[@]")"
Write options as you would write for each program indepently on the command line.
Execute it like:
myscript -s -o pid,tname,time,ucmd -g -d, -u t -t pts/4
You will have to test it more deeply as I just tested some basic conditions. It will fail if the output of pgrep is null (no list of process IPs) as your original command did. Don't know if you wanted that condition to happen.
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',
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%2f485992%2fhow-shall-i-pass-two-groups-of-arguments-from-a-shell-script-to-two-commands%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Pass everyone of the arguments as an independent argument on the command line and take advantage of the option processing loop to decide to with array each argument should be added:
#!/bin/bash
for opt; do
if [[ $opt == -s ]]; then sel=s; continue; fi
if [[ $opt == -g ]]; then sel=g; continue; fi
if [[ $sel == s ]]; then argss+=("$opt");
elif [[ $sel == g ]]; then argsg+=("$opt");
else
echo "There is an error with the value of val=$val"
exit 3
fi
done
echo pgrep "$argsg[@]"
pgrep "$argsg[@]"
echo ps "$argss[@]"
ps "$argss[@]" -p "$(pgrep "$argsg[@]")"
Write options as you would write for each program indepently on the command line.
Execute it like:
myscript -s -o pid,tname,time,ucmd -g -d, -u t -t pts/4
You will have to test it more deeply as I just tested some basic conditions. It will fail if the output of pgrep is null (no list of process IPs) as your original command did. Don't know if you wanted that condition to happen.
add a comment |
up vote
1
down vote
Pass everyone of the arguments as an independent argument on the command line and take advantage of the option processing loop to decide to with array each argument should be added:
#!/bin/bash
for opt; do
if [[ $opt == -s ]]; then sel=s; continue; fi
if [[ $opt == -g ]]; then sel=g; continue; fi
if [[ $sel == s ]]; then argss+=("$opt");
elif [[ $sel == g ]]; then argsg+=("$opt");
else
echo "There is an error with the value of val=$val"
exit 3
fi
done
echo pgrep "$argsg[@]"
pgrep "$argsg[@]"
echo ps "$argss[@]"
ps "$argss[@]" -p "$(pgrep "$argsg[@]")"
Write options as you would write for each program indepently on the command line.
Execute it like:
myscript -s -o pid,tname,time,ucmd -g -d, -u t -t pts/4
You will have to test it more deeply as I just tested some basic conditions. It will fail if the output of pgrep is null (no list of process IPs) as your original command did. Don't know if you wanted that condition to happen.
add a comment |
up vote
1
down vote
up vote
1
down vote
Pass everyone of the arguments as an independent argument on the command line and take advantage of the option processing loop to decide to with array each argument should be added:
#!/bin/bash
for opt; do
if [[ $opt == -s ]]; then sel=s; continue; fi
if [[ $opt == -g ]]; then sel=g; continue; fi
if [[ $sel == s ]]; then argss+=("$opt");
elif [[ $sel == g ]]; then argsg+=("$opt");
else
echo "There is an error with the value of val=$val"
exit 3
fi
done
echo pgrep "$argsg[@]"
pgrep "$argsg[@]"
echo ps "$argss[@]"
ps "$argss[@]" -p "$(pgrep "$argsg[@]")"
Write options as you would write for each program indepently on the command line.
Execute it like:
myscript -s -o pid,tname,time,ucmd -g -d, -u t -t pts/4
You will have to test it more deeply as I just tested some basic conditions. It will fail if the output of pgrep is null (no list of process IPs) as your original command did. Don't know if you wanted that condition to happen.
Pass everyone of the arguments as an independent argument on the command line and take advantage of the option processing loop to decide to with array each argument should be added:
#!/bin/bash
for opt; do
if [[ $opt == -s ]]; then sel=s; continue; fi
if [[ $opt == -g ]]; then sel=g; continue; fi
if [[ $sel == s ]]; then argss+=("$opt");
elif [[ $sel == g ]]; then argsg+=("$opt");
else
echo "There is an error with the value of val=$val"
exit 3
fi
done
echo pgrep "$argsg[@]"
pgrep "$argsg[@]"
echo ps "$argss[@]"
ps "$argss[@]" -p "$(pgrep "$argsg[@]")"
Write options as you would write for each program indepently on the command line.
Execute it like:
myscript -s -o pid,tname,time,ucmd -g -d, -u t -t pts/4
You will have to test it more deeply as I just tested some basic conditions. It will fail if the output of pgrep is null (no list of process IPs) as your original command did. Don't know if you wanted that condition to happen.
answered Dec 5 at 8:54
Isaac
10.9k11648
10.9k11648
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f485992%2fhow-shall-i-pass-two-groups-of-arguments-from-a-shell-script-to-two-commands%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