ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Below is the part of my bash script which uses awk to to fetch a line from a file. Choice is set by a case, and i know it is receiving a proper number because of the echo statement. The problem is with the syntax of the awk command it says the error is with one of the ', but when I run the command at the command line and replace "$choice" with a number it works properly. So I am not sure what is going on.
Let me ask you this, if we changed this slightly to:
command="ls"
$command
What would you expect to be the output from this?
If you answer this you will be able to figure out what is wrong, unless you have skipped some of the code?
Below is the part of my bash script which uses awk to to fetch a line from a file. Choice is set by a case, and i know it is receiving a proper number because of the echo statement. The problem is with the syntax of the awk command it says the error is with one of the ', but when I run the command at the command line and replace "$choice" with a number it works properly. So I am not sure what is going on.
The only method I could kinda get to work was slinx's although instead of reading a single line it reads the entire file. The script takes an input from the user and then translastes the corresponding input into a line number (the line number refers to a second file). The script should read that line and pipe it to the say command so the computer will speak it.
@catkin Am I missing something or should yours at least have an echo at the front of the last line:
Maybe! I was guessing from the OP's
Code:
command="awk 'NR = "$choice"' beerpong.txt | say"
that the intention was for awk to print line number $choice from beerpong.txt to stdout and the say command would take it from standard in and do ... whatever say does.
The steps taken by bash are given here where it says:
... the shell does the following:
[snip]
Breaks the input into words and operators, obeying the quoting rules described in Quoting. These tokens are separated by metacharacters. Alias expansion ... [snip].
Parses the tokens into simple and compound commands (see Shell Commands).
Performs the various shell expansions (see Shell Expansions), breaking the expanded tokens into lists of filenames (see Filename Expansion) and commands and arguments.
Performs any necessary redirections ... [snip]
Executes the command
[snip]
Assuming the value of $choice is 4 and no aliases apply then the value of $command is
Code:
awk 'NR = 4' beerpong.txt | say
When the second line of the code snippet is run by bash, this is what bash does
[snip]
Examines the command (line) and finds a single token (word-or-operator) that is a word. The command is unchanged; it is still $command.
Examines the command for
Pipelines identified by "|"
Lists identified by ";", "&", "&&" or "||"
Compound commands identified by "until", "while" or "for"
Conditional commands identified by "if", "case", "select", "(( ))" or "[[ ]]"
Grouping commands identified by "( )" or "{ }"
Coprocesses identified by "coproc"
None of the above apply. The command is unchanged; it is still $command.
Expands $command. The command is now
Code:
awk 'NR = 4' beerpong.txt | say
Looks for redirection and finds none. The command is not changed.
Executes the command: runs awk with arguments
'NR = 4'
beerpong.txt
|
say
[snip]
When the awk command runs, it borks on the first argument. Note: when awk 'NR = 4' beerpong.txt is run at the command prompt it works because bash strips off the single quotes and runs awk with two arguments: NR = 4 and beerpong.txt.
How to fix?
"NR = 4" can be corrected to "NR == 4"
"NR == 4" can conveniently be made a single word by changing it to NR==4.
Move the pipe symbol and the say command out of $command
Code:
command="awk NR==$choice beerpong.txt"
$command | say
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.