Script with more than one flag2019 Community Moderator ElectionHow to cd into more than one directory?More shell scripts than one in one text file?Sort command on more than one fieldLog output from more than one scriptHow do I exec more than one command?Call one shell script with anotherOrganizing one script with anotherSorting more than one columnLinux -more than one root accountUsing 'read' for more than one variable
Mixing PEX brands
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
Quoting Keynes in a lecture
Redundant comparison & "if" before assignment
Why did the EU agree to delay the Brexit deadline?
Can disgust be a key component of horror?
Why would a new[] expression ever invoke a destructor?
Terse Method to Swap Lowest for Highest?
Can I still be respawned if I die by falling off the map?
Why "had" in "[something] we would have made had we used [something]"?
Calculating total slots
Why is this estimator biased?
Why is the "ls" command showing permissions of files in a FAT32 partition?
Calculate sum of polynomial roots
Why does a simple loop result in ASYNC_NETWORK_IO waits?
How can I write humor as character trait?
Why is it that I can sometimes guess the next note?
Does the UK parliament need to pass secondary legislation to accept the Article 50 extension
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
Is there an injective, monotonically increasing, strictly concave function from the reals, to the reals?
How could a planet have erratic days?
Can a College of Swords bard use a Blade Flourish option on an opportunity attack provoked by their own Dissonant Whispers spell?
Script with more than one flag
2019 Community Moderator ElectionHow to cd into more than one directory?More shell scripts than one in one text file?Sort command on more than one fieldLog output from more than one scriptHow do I exec more than one command?Call one shell script with anotherOrganizing one script with anotherSorting more than one columnLinux -more than one root accountUsing 'read' for more than one variable
I am trying to create a script that lowercases/uppercases files or directories.
modify [-r] [-l|-u] <dir/file names...>
I am currently using getopts to check for flags. However, I can't run my command like that modify -rl to lowercase recursively my dir / files.
(edit) The code:
#!/bin/bash
FLAGS=""
if [ $# -eq 0 ]
then
echo -e "No arguments supplied. nPlease supply an argument.For more information use -h."
else
while getopts "rluh" opt
do
case $opt in
r)
FLAGS+=r
;;
l)
FLAGS+=l
;;
u)
FLAGS+=u
;;
h)
FLAGS+=h
;;
*)
echo "Unrecognized argument. Please enter -h for information"
esac
done
fi
if [[ "$FLAGS" == "l" && "$2" -eq 0 ]]
then
echo "file to modify $2"
elif [[ "$FLAGS" == "h" && "$2" -eq 0 ]]
then
echo "Some info"
fi
exit 0
linux bash shell-script arguments
New contributor
add a comment |
I am trying to create a script that lowercases/uppercases files or directories.
modify [-r] [-l|-u] <dir/file names...>
I am currently using getopts to check for flags. However, I can't run my command like that modify -rl to lowercase recursively my dir / files.
(edit) The code:
#!/bin/bash
FLAGS=""
if [ $# -eq 0 ]
then
echo -e "No arguments supplied. nPlease supply an argument.For more information use -h."
else
while getopts "rluh" opt
do
case $opt in
r)
FLAGS+=r
;;
l)
FLAGS+=l
;;
u)
FLAGS+=u
;;
h)
FLAGS+=h
;;
*)
echo "Unrecognized argument. Please enter -h for information"
esac
done
fi
if [[ "$FLAGS" == "l" && "$2" -eq 0 ]]
then
echo "file to modify $2"
elif [[ "$FLAGS" == "h" && "$2" -eq 0 ]]
then
echo "Some info"
fi
exit 0
linux bash shell-script arguments
New contributor
Is this a question about how to handle the command line options usinggetopts
or about how to implement lowercasing of filenames? In either case, you have not shown any actual code.
– Kusalananda
yesterday
Sorry for that. It is actually a question about how to handle options using getopts.
– ClemHlrdt
yesterday
add a comment |
I am trying to create a script that lowercases/uppercases files or directories.
modify [-r] [-l|-u] <dir/file names...>
I am currently using getopts to check for flags. However, I can't run my command like that modify -rl to lowercase recursively my dir / files.
(edit) The code:
#!/bin/bash
FLAGS=""
if [ $# -eq 0 ]
then
echo -e "No arguments supplied. nPlease supply an argument.For more information use -h."
else
while getopts "rluh" opt
do
case $opt in
r)
FLAGS+=r
;;
l)
FLAGS+=l
;;
u)
FLAGS+=u
;;
h)
FLAGS+=h
;;
*)
echo "Unrecognized argument. Please enter -h for information"
esac
done
fi
if [[ "$FLAGS" == "l" && "$2" -eq 0 ]]
then
echo "file to modify $2"
elif [[ "$FLAGS" == "h" && "$2" -eq 0 ]]
then
echo "Some info"
fi
exit 0
linux bash shell-script arguments
New contributor
I am trying to create a script that lowercases/uppercases files or directories.
modify [-r] [-l|-u] <dir/file names...>
I am currently using getopts to check for flags. However, I can't run my command like that modify -rl to lowercase recursively my dir / files.
(edit) The code:
#!/bin/bash
FLAGS=""
if [ $# -eq 0 ]
then
echo -e "No arguments supplied. nPlease supply an argument.For more information use -h."
else
while getopts "rluh" opt
do
case $opt in
r)
FLAGS+=r
;;
l)
FLAGS+=l
;;
u)
FLAGS+=u
;;
h)
FLAGS+=h
;;
*)
echo "Unrecognized argument. Please enter -h for information"
esac
done
fi
if [[ "$FLAGS" == "l" && "$2" -eq 0 ]]
then
echo "file to modify $2"
elif [[ "$FLAGS" == "h" && "$2" -eq 0 ]]
then
echo "Some info"
fi
exit 0
linux bash shell-script arguments
linux bash shell-script arguments
New contributor
New contributor
edited yesterday
Rui F Ribeiro
41.6k1483141
41.6k1483141
New contributor
asked yesterday
ClemHlrdtClemHlrdt
113
113
New contributor
New contributor
Is this a question about how to handle the command line options usinggetopts
or about how to implement lowercasing of filenames? In either case, you have not shown any actual code.
– Kusalananda
yesterday
Sorry for that. It is actually a question about how to handle options using getopts.
– ClemHlrdt
yesterday
add a comment |
Is this a question about how to handle the command line options usinggetopts
or about how to implement lowercasing of filenames? In either case, you have not shown any actual code.
– Kusalananda
yesterday
Sorry for that. It is actually a question about how to handle options using getopts.
– ClemHlrdt
yesterday
Is this a question about how to handle the command line options using
getopts
or about how to implement lowercasing of filenames? In either case, you have not shown any actual code.– Kusalananda
yesterday
Is this a question about how to handle the command line options using
getopts
or about how to implement lowercasing of filenames? In either case, you have not shown any actual code.– Kusalananda
yesterday
Sorry for that. It is actually a question about how to handle options using getopts.
– ClemHlrdt
yesterday
Sorry for that. It is actually a question about how to handle options using getopts.
– ClemHlrdt
yesterday
add a comment |
3 Answers
3
active
oldest
votes
It's easier if you set individual flags for each option that is activated, so that you don't have yet another string to parse just to check whether an option was used.
For example:
#!/bin/bash
self=$0
show_help ()
cat <<END_HELP
Usage: $self [-r] [-l
recurse=0 # don't recurse by default
lowercase=1 # lowercase by default
while getopts rluh opt; do
case $opt in
r) recurse=1 ;;
l) lowercase=1 ;;
u) lowercase=0 ;;
h) show_help
exit ;;
*) echo 'Error in parsing options' >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
for pathname do
if [ -d "$pathname" ] && [ "$recurse" -eq 1 ]; then
# recurse here
fi
if [ "$lowercase" -eq 1 ]; then
# turn into lowercase
else
# turn into uppercase
fi
done
Note that the code after the shift
is just example code. I would suggest that you instead of a loop considered using find
for doing recursion (possibly in all cases, even if the recursion
flag isn't set).
The shift
, by the way, gets rid of all the parsed options from "$@"
so that only pathname operands are left in the list of positional parameters. This is what the loop afterwards iterates over.
The code, as written above uses sensible defaults (these should be mentioned in the help text). This means that it's perfectly ok to run the script without any options. As a side note, running the tool with no arguments whatsoever should probably not result in an error. There's no work to do, so no work should be done.
Or usefalse
/true
instead of0
/1
and use"$recurse"
instead of[ "$recurse" -eq 1 ]
– Stéphane Chazelas
yesterday
add a comment |
You're concatenating the flag letters to a single string, so with myscript -l -r
, you get FLAGS=lr
, which isn't equal to either l
or r
.
You could use pattern matching instead of equality testing to deal with that:
if [[ $FLAGS = *r* ]]; then
echo "flag -r was given"
or add distinct variables for each flag:
r_flag=
l_flag=
while getopts "rluh" opt; do
case $opt in
r) r_flag=1;;
...
if [[ $r_flag ]]; then
echo "flag -r was given"
In any case, you're very likely to want to shift $(( OPTIND - 1 ))
after the while getopts
loop, so that the options handled by getopts
are removed from the positional parameters, and what's left in $1
, $2
... are the script arguments given after the options.
add a comment |
Something like FLAGS+=r
will append r to FLAGS. For modify -r -l
FLAGS therefore will be rl
. You should use one variable per option (FLAG_r
).
The comparision "$2" -eq 0
will only work if the script gets two directory names, with the second one being 0
.
A better comparision would be "$2" = ""
.
New contributor
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
);
);
ClemHlrdt is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507763%2fscript-with-more-than-one-flag%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
It's easier if you set individual flags for each option that is activated, so that you don't have yet another string to parse just to check whether an option was used.
For example:
#!/bin/bash
self=$0
show_help ()
cat <<END_HELP
Usage: $self [-r] [-l
recurse=0 # don't recurse by default
lowercase=1 # lowercase by default
while getopts rluh opt; do
case $opt in
r) recurse=1 ;;
l) lowercase=1 ;;
u) lowercase=0 ;;
h) show_help
exit ;;
*) echo 'Error in parsing options' >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
for pathname do
if [ -d "$pathname" ] && [ "$recurse" -eq 1 ]; then
# recurse here
fi
if [ "$lowercase" -eq 1 ]; then
# turn into lowercase
else
# turn into uppercase
fi
done
Note that the code after the shift
is just example code. I would suggest that you instead of a loop considered using find
for doing recursion (possibly in all cases, even if the recursion
flag isn't set).
The shift
, by the way, gets rid of all the parsed options from "$@"
so that only pathname operands are left in the list of positional parameters. This is what the loop afterwards iterates over.
The code, as written above uses sensible defaults (these should be mentioned in the help text). This means that it's perfectly ok to run the script without any options. As a side note, running the tool with no arguments whatsoever should probably not result in an error. There's no work to do, so no work should be done.
Or usefalse
/true
instead of0
/1
and use"$recurse"
instead of[ "$recurse" -eq 1 ]
– Stéphane Chazelas
yesterday
add a comment |
It's easier if you set individual flags for each option that is activated, so that you don't have yet another string to parse just to check whether an option was used.
For example:
#!/bin/bash
self=$0
show_help ()
cat <<END_HELP
Usage: $self [-r] [-l
recurse=0 # don't recurse by default
lowercase=1 # lowercase by default
while getopts rluh opt; do
case $opt in
r) recurse=1 ;;
l) lowercase=1 ;;
u) lowercase=0 ;;
h) show_help
exit ;;
*) echo 'Error in parsing options' >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
for pathname do
if [ -d "$pathname" ] && [ "$recurse" -eq 1 ]; then
# recurse here
fi
if [ "$lowercase" -eq 1 ]; then
# turn into lowercase
else
# turn into uppercase
fi
done
Note that the code after the shift
is just example code. I would suggest that you instead of a loop considered using find
for doing recursion (possibly in all cases, even if the recursion
flag isn't set).
The shift
, by the way, gets rid of all the parsed options from "$@"
so that only pathname operands are left in the list of positional parameters. This is what the loop afterwards iterates over.
The code, as written above uses sensible defaults (these should be mentioned in the help text). This means that it's perfectly ok to run the script without any options. As a side note, running the tool with no arguments whatsoever should probably not result in an error. There's no work to do, so no work should be done.
Or usefalse
/true
instead of0
/1
and use"$recurse"
instead of[ "$recurse" -eq 1 ]
– Stéphane Chazelas
yesterday
add a comment |
It's easier if you set individual flags for each option that is activated, so that you don't have yet another string to parse just to check whether an option was used.
For example:
#!/bin/bash
self=$0
show_help ()
cat <<END_HELP
Usage: $self [-r] [-l
recurse=0 # don't recurse by default
lowercase=1 # lowercase by default
while getopts rluh opt; do
case $opt in
r) recurse=1 ;;
l) lowercase=1 ;;
u) lowercase=0 ;;
h) show_help
exit ;;
*) echo 'Error in parsing options' >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
for pathname do
if [ -d "$pathname" ] && [ "$recurse" -eq 1 ]; then
# recurse here
fi
if [ "$lowercase" -eq 1 ]; then
# turn into lowercase
else
# turn into uppercase
fi
done
Note that the code after the shift
is just example code. I would suggest that you instead of a loop considered using find
for doing recursion (possibly in all cases, even if the recursion
flag isn't set).
The shift
, by the way, gets rid of all the parsed options from "$@"
so that only pathname operands are left in the list of positional parameters. This is what the loop afterwards iterates over.
The code, as written above uses sensible defaults (these should be mentioned in the help text). This means that it's perfectly ok to run the script without any options. As a side note, running the tool with no arguments whatsoever should probably not result in an error. There's no work to do, so no work should be done.
It's easier if you set individual flags for each option that is activated, so that you don't have yet another string to parse just to check whether an option was used.
For example:
#!/bin/bash
self=$0
show_help ()
cat <<END_HELP
Usage: $self [-r] [-l
recurse=0 # don't recurse by default
lowercase=1 # lowercase by default
while getopts rluh opt; do
case $opt in
r) recurse=1 ;;
l) lowercase=1 ;;
u) lowercase=0 ;;
h) show_help
exit ;;
*) echo 'Error in parsing options' >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
for pathname do
if [ -d "$pathname" ] && [ "$recurse" -eq 1 ]; then
# recurse here
fi
if [ "$lowercase" -eq 1 ]; then
# turn into lowercase
else
# turn into uppercase
fi
done
Note that the code after the shift
is just example code. I would suggest that you instead of a loop considered using find
for doing recursion (possibly in all cases, even if the recursion
flag isn't set).
The shift
, by the way, gets rid of all the parsed options from "$@"
so that only pathname operands are left in the list of positional parameters. This is what the loop afterwards iterates over.
The code, as written above uses sensible defaults (these should be mentioned in the help text). This means that it's perfectly ok to run the script without any options. As a side note, running the tool with no arguments whatsoever should probably not result in an error. There's no work to do, so no work should be done.
edited yesterday
answered yesterday
KusalanandaKusalananda
137k17258426
137k17258426
Or usefalse
/true
instead of0
/1
and use"$recurse"
instead of[ "$recurse" -eq 1 ]
– Stéphane Chazelas
yesterday
add a comment |
Or usefalse
/true
instead of0
/1
and use"$recurse"
instead of[ "$recurse" -eq 1 ]
– Stéphane Chazelas
yesterday
Or use
false
/true
instead of 0
/1
and use "$recurse"
instead of [ "$recurse" -eq 1 ]
– Stéphane Chazelas
yesterday
Or use
false
/true
instead of 0
/1
and use "$recurse"
instead of [ "$recurse" -eq 1 ]
– Stéphane Chazelas
yesterday
add a comment |
You're concatenating the flag letters to a single string, so with myscript -l -r
, you get FLAGS=lr
, which isn't equal to either l
or r
.
You could use pattern matching instead of equality testing to deal with that:
if [[ $FLAGS = *r* ]]; then
echo "flag -r was given"
or add distinct variables for each flag:
r_flag=
l_flag=
while getopts "rluh" opt; do
case $opt in
r) r_flag=1;;
...
if [[ $r_flag ]]; then
echo "flag -r was given"
In any case, you're very likely to want to shift $(( OPTIND - 1 ))
after the while getopts
loop, so that the options handled by getopts
are removed from the positional parameters, and what's left in $1
, $2
... are the script arguments given after the options.
add a comment |
You're concatenating the flag letters to a single string, so with myscript -l -r
, you get FLAGS=lr
, which isn't equal to either l
or r
.
You could use pattern matching instead of equality testing to deal with that:
if [[ $FLAGS = *r* ]]; then
echo "flag -r was given"
or add distinct variables for each flag:
r_flag=
l_flag=
while getopts "rluh" opt; do
case $opt in
r) r_flag=1;;
...
if [[ $r_flag ]]; then
echo "flag -r was given"
In any case, you're very likely to want to shift $(( OPTIND - 1 ))
after the while getopts
loop, so that the options handled by getopts
are removed from the positional parameters, and what's left in $1
, $2
... are the script arguments given after the options.
add a comment |
You're concatenating the flag letters to a single string, so with myscript -l -r
, you get FLAGS=lr
, which isn't equal to either l
or r
.
You could use pattern matching instead of equality testing to deal with that:
if [[ $FLAGS = *r* ]]; then
echo "flag -r was given"
or add distinct variables for each flag:
r_flag=
l_flag=
while getopts "rluh" opt; do
case $opt in
r) r_flag=1;;
...
if [[ $r_flag ]]; then
echo "flag -r was given"
In any case, you're very likely to want to shift $(( OPTIND - 1 ))
after the while getopts
loop, so that the options handled by getopts
are removed from the positional parameters, and what's left in $1
, $2
... are the script arguments given after the options.
You're concatenating the flag letters to a single string, so with myscript -l -r
, you get FLAGS=lr
, which isn't equal to either l
or r
.
You could use pattern matching instead of equality testing to deal with that:
if [[ $FLAGS = *r* ]]; then
echo "flag -r was given"
or add distinct variables for each flag:
r_flag=
l_flag=
while getopts "rluh" opt; do
case $opt in
r) r_flag=1;;
...
if [[ $r_flag ]]; then
echo "flag -r was given"
In any case, you're very likely to want to shift $(( OPTIND - 1 ))
after the while getopts
loop, so that the options handled by getopts
are removed from the positional parameters, and what's left in $1
, $2
... are the script arguments given after the options.
answered yesterday
ilkkachuilkkachu
62.4k10103179
62.4k10103179
add a comment |
add a comment |
Something like FLAGS+=r
will append r to FLAGS. For modify -r -l
FLAGS therefore will be rl
. You should use one variable per option (FLAG_r
).
The comparision "$2" -eq 0
will only work if the script gets two directory names, with the second one being 0
.
A better comparision would be "$2" = ""
.
New contributor
add a comment |
Something like FLAGS+=r
will append r to FLAGS. For modify -r -l
FLAGS therefore will be rl
. You should use one variable per option (FLAG_r
).
The comparision "$2" -eq 0
will only work if the script gets two directory names, with the second one being 0
.
A better comparision would be "$2" = ""
.
New contributor
add a comment |
Something like FLAGS+=r
will append r to FLAGS. For modify -r -l
FLAGS therefore will be rl
. You should use one variable per option (FLAG_r
).
The comparision "$2" -eq 0
will only work if the script gets two directory names, with the second one being 0
.
A better comparision would be "$2" = ""
.
New contributor
Something like FLAGS+=r
will append r to FLAGS. For modify -r -l
FLAGS therefore will be rl
. You should use one variable per option (FLAG_r
).
The comparision "$2" -eq 0
will only work if the script gets two directory names, with the second one being 0
.
A better comparision would be "$2" = ""
.
New contributor
New contributor
answered yesterday
Uwe OhseUwe Ohse
1013
1013
New contributor
New contributor
add a comment |
add a comment |
ClemHlrdt is a new contributor. Be nice, and check out our Code of Conduct.
ClemHlrdt is a new contributor. Be nice, and check out our Code of Conduct.
ClemHlrdt is a new contributor. Be nice, and check out our Code of Conduct.
ClemHlrdt is a new contributor. Be nice, and check out our Code of Conduct.
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%2f507763%2fscript-with-more-than-one-flag%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
Is this a question about how to handle the command line options using
getopts
or about how to implement lowercasing of filenames? In either case, you have not shown any actual code.– Kusalananda
yesterday
Sorry for that. It is actually a question about how to handle options using getopts.
– ClemHlrdt
yesterday