•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 423,103 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,405 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 809 | Replies: 6
![]() |
Perhaps use case/esac with a pattern of [a-z]+
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
•
•
Join Date: Apr 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 8
Try this:
Not exactly sure what you want your end result to be - but this for instance should find uppercase letters - if they are there, convert them to lowercase.
Basically, doing the grep for [A-Z] returns the argument provided if it has capital letters. If this happens, the return code ($?) is 0. If the return code is a 1, no capital letters were in the argument.
You can then run through all arguments.
You can apply this anyway you want though. Grep for [a-z] or [A-Z]. If the return code is 0, then the caps or lowercase were found and you can act accordingly.
function test_upper {
test=`echo $1 | grep [A-Z]`
if [ "$?" = "0" ]; then
#Uppercase letters found.
#translate to lower case:
echo $test | tr '[A-Z]' '[a-z]
else
#letters are all lowercase, return them.
echo $1
fi
}Basically, doing the grep for [A-Z] returns the argument provided if it has capital letters. If this happens, the return code ($?) is 0. If the return code is a 1, no capital letters were in the argument.
You can then run through all arguments.
You can apply this anyway you want though. Grep for [a-z] or [A-Z]. If the return code is 0, then the caps or lowercase were found and you can act accordingly.
•
•
Join Date: Apr 2008
Posts: 39
Reputation:
Rep Power: 1
Solved Threads: 8
with my above function:
... or
if you want something simpler.
You could, for instance, rewrite the test_upper function to return a 1 or a 0, depending on whether or not it found uppercase. (You could simple use "echo $?" to "return" this value). Then, when you loop the arguments, you can have a variable that holds whether or not the case you are testing for was found (globally). e.g.
while [ "$*" != "" ]
do
working_arg=`test_upper $1`
if [ "x${working_arg}x" != "xx" ]; then
echo "$working_arg is all lowercase."
fi
shift
donefor arg in "$*" do test_upper $arg done
You could, for instance, rewrite the test_upper function to return a 1 or a 0, depending on whether or not it found uppercase. (You could simple use "echo $?" to "return" this value). Then, when you loop the arguments, you can have a variable that holds whether or not the case you are testing for was found (globally). e.g.
CAPS=0
for arg in "$*"
do
if [ `test_upper $arg` -eq 0 ]
then
CAPS=1
fi
done
if [ $CAPS -eq 1 ]
then
#blah blah blah
fi Last edited by omrsafetyo : Jul 24th, 2008 at 2:55 pm.
•
•
Join Date: Apr 2006
Posts: 140
Reputation:
Rep Power: 3
Solved Threads: 26
Look at the bash guide here. It has examples on checking for upper case.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
Other Threads in the Shell Scripting Forum
- Previous Thread: print only the last entry of the file
- Next Thread: Help!!!!!!



Linear Mode