LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-07-2015, 05:37 PM   #1
karrolli
LQ Newbie
 
Registered: Nov 2015
Posts: 5

Rep: Reputation: Disabled
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
 
Old 11-07-2015, 08:19 PM   #2
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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.

Last edited by berndbausch; 11-07-2015 at 08:42 PM.
 
Old 11-08-2015, 03:50 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
As shown by berndbausch, please use [code][/code] tags around code and data.
 
Old 11-08-2015, 06:35 AM   #4
karrolli
LQ Newbie
 
Registered: Nov 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-08-2015, 06:42 AM   #5
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by karrolli View Post
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).
 
Old 11-08-2015, 07:02 AM   #6
karrolli
LQ Newbie
 
Registered: Nov 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
When I run myscript, I have already entered a number and it is after that when it hangs.
 
Old 11-08-2015, 09:06 AM   #7
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora
Posts: 1,687

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Originally Posted by karrolli View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
variable expansion and pathname expansion ShadeLover Linux - General 6 04-22-2015 11:56 PM
simple parameter expansion casperdaghost Linux - Newbie 3 09-12-2013 11:53 AM
[SOLVED] Convert length-indicated variable length record file to LF-terminated Z038 Linux - General 10 11-30-2012 12:59 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 05:54 AM
getting rpoblem in assigning value to variable in if construct Ankit mittal Programming 9 01-07-2006 10:12 AM


All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration