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 |
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.
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 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. |
As shown by berndbausch, please use [code][/code] tags around code and data.
|
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 |
Quote:
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). |
When I run myscript, I have already entered a number and it is after that when it hangs.
|
Quote:
Code:
read -p "Please enter the word length: " length 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. |