Bash Script

olando

New Member
Joined
Feb 27, 2022
Messages
1
Reaction score
0
Credits
17
Bash: Pattern Matching
Given an array of strings, count the strings
that contain at least one uppercase character
and output the result to stdout.
Example:
my array=[FirstWord" "Word2" "thirdiword']
The match_uppercase function will return 2
since "FirstWord" and "Word2" each contain
an uppercase letter.

My solution
1#1/usr/bin/env bash
3 Input array is read and stored in to my_array variable.
#You can view the code by pressing> button above.
Code:
function match_uppercase()
typeset-a data-("se")
#Write your code here
Let count = 0
for upper in $data[@] ;
do
if I[ Supper [@) [A-Z] ;; then
echo $((count+1))
fi
done

Sample inpu and output
The match uppercase function will return 2
since "FirstWord" and "Word2" each contain
an uppercase letter.
Input Format For Custom Testing
The only line that contains the elements of
myarray
Sample Case
Sample Input For Custom Testing
Abc
bcd
Efs
def
cDe
Sample Output
Explanation
There are 3 strings containing at least one
uppercase character: Abc', "Efg". and
TCDe".
 


It looks good though, but there were errors, so it couldn't run.
It doesn't look good at all! It's badly thought out nonsense.
There are a number of syntax errors in the OP's code.

Take a look at this:
Bash:
#!/usr/bin/env bash
wordsIn=("FirstWord" "woRd2" "thirdword")
wordsOut=() #empty array
count=0
for word in ${wordsIn[@]}; do
    if [[ "$word" =~ [[:upper:]] ]]; then
        wordsOut+=("$word")
        count=$((count+1))
    fi
done
echo "$count words contained uppercase letters:"
echo "${wordsOut[*]}"
Above sets an array of words called wordsIn - those are the words we'll check.
Then we set up an empty array called wordsOut to store any words we find that contain uppercase characters. And create a variable called count to count the number of words that match our pattern.
Then we loop through each word in the array of wordsIn and try to determine whether the word contains any uppercase characters.
I don't think =~ [A-Z] on its own is enough to identify uppercase characters, so it's better to use =~ [[:upper:]] instead.
If the current word contains uppercase characters, we add the word to wordsOut.

Finally, once we've iterated through all of the words, we output the number of words that contained one or more uppercase characters AND list them.


And if you want to use a function:
Bash:
#!/usr/bin/env bash
function match_uppercase()
{
    wordsIn=("$@")
    wordsOut=()
    count=0
    for word in ${wordsIn[@]}; do
        if [[ "$word" =~ [[:upper:]] ]]; then
            wordsOut+=("${word}")
            count=$((count+1))
        fi
    done
    echo "$count words contained uppercase letters:"
    echo "${wordsOut[*]}"
}

words=("FirstWord" "woRd2" "thirdword")
match_uppercase ${words[@]}

In the above code, we have defined a function called match_uppercase(), which does pretty much exactly what we did in the previous code block.
This time, we store all of the parameters received by the function in an array called wordsIn.

After the function definition, in the main block, we set up an array of words to check and then call the function, passing the array of words as parameters to the function.
 
Last edited:
It doesn't look good at all! It's badly though out nonsense.
There are a number of syntax errors in the OP's code.

Take a look at this:
Bash:
#!/usr/bin/env bash
wordsIn=("FirstWord" "woRd2" "thirdword")
wordsOut=() #empty array
count=0
for word in ${wordsIn[@]}; do
    if [[ "$word" =~ [[:upper:]] ]]; then
        wordsOut+=("$word")
        count=$((count+1))
    fi
done
echo "$count words contained uppercase letters:"
echo "${wordsOut[*]}"
Above sets an array of words called wordsIn - those are the words we'll check.
Then we set up an empty array called wordsOut to store any words we find that contain uppercase characters. And create a variable called count to count the number of words that match our pattern.
Then we loop through each word in the array of wordsIn and try to determine whether the word contains any uppercase characters.
I don't think =~ [A-Z] on its own is enough to identify uppercase characters, so it's better to use =~ [[:upper:]] instead.
If the current word contains uppercase characters, we add the word to wordsOut.

Finally, once we've iterated through all of the words, we output the number of words that contained one or more uppercase characters AND list them.


And if you want to use a function:
Bash:
#!/usr/bin/env bash
function match_uppercase()
{
    wordsIn=("$@")
    wordsOut=()
    count=0
    for word in ${wordsIn[@]}; do
        if [[ "$word" =~ [[:upper:]] ]]; then
            wordsOut+=("${word}")
            count=$((count+1))
        fi
    done
    echo "$count words contained uppercase letters:"
    echo "${wordsOut[*]}"
}

words=("FirstWord" "woRd2" "thirdword")
match_uppercase ${words[@]}

In the above code, we have defined a function called match_uppercase(), which does pretty much exactly what we did in the previous code block.
This time, we store all of the parameters received by the function in an array called wordsIn.

After the function definition, in the main block, we set up an array of words to check and then call the function, passing the array of words as parameters to the function.
And what is the "@$" doing in your function?
 
And what is the "@$" doing in your function?
You mean $@?
That refers to all of the parameters received by the function.
So the line:
wordsIn=("$@") stores all of the parameters received by the function in an array called wordsIn.

When the function is called in this line:
match_uppercase ${words[@]}
We call the function and pass the array of strings $words. The bash interpreter will then effectively expand the array and make the function call look like this:
match_uppercase FirstWord woRd2 thirdword

So each word in the array gets passed to the function as a separate parameter.

Which is why I’m copying all of the parameters received by the function into another array inside the function.
 
You mean $@?
That refers to all of the parameters received by the function.
So the line:
wordsIn=("$@") stores all of the parameters received by the function in an array called wordsIn.

When the function is called in this line:
match_uppercase ${words[@]}
We call the function and pass the array of strings $words. The bash interpreter will then effectively expand the array and make the function call look like this:
match_uppercase FirstWord woRd2 thirdword

So each word in the array gets passed to the function as a separate parameter.

Which is why I’m copying all of the parameters received by the function into another array inside the function.
In this example, are arguments the same as parameters, or is the argument the second parameter and so on?
 
In this example, are arguments the same as parameters, or is the argument the second parameter and so on?

If I’m understanding your question correctly:

Arguments, or parameters are exactly the same thing.Some people prefer the term arguments, others prefer the term parameters.

Neither of the scripts I posted accept parameters/arguments. More accurately, if the scripts are supplied with any parameters/arguments, the script ignores them, it doesn’t do anything with them.

So in the scope of the main script, arguments/parameters passed to the main script are simply ignored.

But our function does accept parameters/arguments, it does process them. So inside the function, the variable $@ refers to all of the parameters/arguments passed to the function when it is called.

Just as parameters/arguments to scripts are stored in local variables called $1 to $n (where n is the total number of arguments/parameters). And $0 is the name/path of the script. And $# is the number of parameters. Etc etc

Parameters/arguments received by functions are also stored in local variables called $1 to $n. And $0 will be the name of the function. etc…

Any reference to $1 in the main body of a script refers to the first argument/parameter passed to the script when it was ran.

But inside a function $1
refers to the first parameter/argument that was passed to the function when it was called. It does NOT refer to the first parameter that was passed to the script.

The variables may have the same names, but they have different scope. One only exists inside the function and is only visible inside the function.
The other exists at the next level up, inside the main script.
Does that make sense?!
 
Does that make sense?!

Yeah it does, i'm just trying to sort it out, i use $1 all the time in scripts and aliases. i was just trying to figure this out cuz in my bash notes i have:

Code:
Each command line string is an argument:

cp -r <file>

"cp" is argument 0, "-r" is 1, file is 2. "-r" is an option.
File is also a parameter, parameters supply information for the
commands or input.

but i suppose arguments are parameters, but a parameter can also be said to be everything after the command...
 
Arguments, or parameters are exactly the same thing.
Parameters can thought of as space holders, and arguments thought of as what takes the space.
For example, in function (x, y), x and y are parameters and the values that get put into
them are arguments. Hence, in function(x=1, y=2), x and y are the parameters and 1 and 2 are the arguments. The terms however are used interchangeably a lot of the time.
 

Staff online

Members online


Top