LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What parameter expansion construct do I use to show the length of a variable (https://www.linuxquestions.org/questions/linux-newbie-8/what-parameter-expansion-construct-do-i-use-to-show-the-length-of-a-variable-4175558280/)

karrolli 11-07-2015 04:37 PM

What parameter expansion construct do I use to show the length of a variable
 
I must use while loop and if statement but I can't get myscript to work.

#!/bin/bash
#Prints all words from /module9/random_words of a length specified by the user.
read -p "Please enter the word length: " i

while wc -m -eq i /module9/random_words;
do
i=* #Executed as long as condition is true and/or, up to a disaster-condition if any.

if $i != *
then
break #Abandon the while loop.
fi
echo $i; #While good and, no disaster-condition.
done

berndbausch 11-07-2015 07:19 PM

See https://www.gnu.org/software/bash/ma...eter-Expansion

It's the hash sign, e.g. ${#var} is the length of var.

Your script has issues which show that you need to do some basic studying of bash programming.
  • "wc -m -eq i /...." runs wc with -m -eq etc as parameters, which fails because wc doesn't have such parameters. You want to do something else, but I am not sure what.
  • "i=*" will assing the list of all filenames in the current directory (except those starting with a dot) to the variable i. Not your intention I think.
  • The "!=" operator needs to be in double brackets like this "[[ operand1 != operand2 ]]", or double parentheses.
  • On both sides of the != operator, there is a lot of white space, which would have to be quoted. But this test condition is not what you want anyway.

To print all words of a specific length with a shell script you could loop over all lines, then loop over the words in each line, checking for their length. Something like:

Code:

read length
while read line
do
  set $line    # sets positional parameters $1, $2 etc to the words in the line

  # Using ((...)) tests for arithmetic comparisons
  while (( $# > 0 ))
  do
    if (( ${#1} == $length ))
    then echo $1
    fi
    shift    # next word in the line
  done
done < /module9/random_words

Note that this is unlikely to work out of the box.

Perhaps an awk program would be simpler.

I realize that I have probably done your homework. As an exercise, try to find all the constructs and concepts my little program uses in the shell reference guide (link above) or one of the many shell tutorials you find on the internet.

grail 11-08-2015 02:50 AM

As shown by berndbausch, please use [code][/code] tags around code and data.

karrolli 11-08-2015 05:35 AM

Asking for input
 
Do I use the word 'length' when asking to input number of characters? Myscript is hanging. Here is the portion for input.

Code:

read -p "Please enter the word length: " length
read length
while read line


berndbausch 11-08-2015 05:42 AM

Quote:

Originally Posted by karrolli (Post 5446420)
Do I use the word 'length' when asking to input number of characters? Myscript is hanging. Here is the portion for input.

Code:

read -p "Please enter the word length: " length
read length
while read line


It's not hanging. It's waiting for your input. Remove the second read and it will work as expected.

You don't have to use the word length, but it's more intuitive. If you want, you can say laenge, longueur or nagasa depending on the language you are familiar with. Or numchar or fourmoreyears (the latter not being very intuitive).

karrolli 11-08-2015 06:02 AM

When I run myscript, I have already entered a number and it is after that when it hangs.

berndbausch 11-08-2015 08:06 AM

Quote:

Originally Posted by karrolli (Post 5446430)
When I run myscript, I have already entered a number and it is after that when it hangs.

Look at your code:

Code:

read -p "Please enter the word length: " length
read length

Your script reads the length twice. If you input it only once, the script appears to hang, but it really waits for your second input.

Please spend some time learning about bash programming. Trying to debug your script is a waste of time if you don't understand elementary concepts.


All times are GMT -5. The time now is 03:04 AM.