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.
As a learning exercise I wrote this simple bash program.
Code:
#!/bin/bash
# Daniel B. Martin Nov12
#
# To execute this program, launch a terminal session and enter:
# bash /home/daniel/Desktop/LQfiles/dbm520.bin
#
# This program makes a list of words containing a user-specified
# search string such as DGE which is found in BADGE, LEDGE, MIDGET, etc.
# File identification
InFile="/home/daniel/Desktop/Voters/wordlist2.txt"
OutFile="/home/daniel/Desktop/LQfiles/dbm520out.txt"
echo "Writing to" $OutFile
echo "Reading from" $InFile
echo
echo
echo "Please enter a search string such as DGE"
echo " ... (or null to quit.) "
read SrchArg
echo "You entered:" $SrchArg
LSA=${#SrchArg}
echo "The Search Argument length is: " $LSA
if [ $LSA=0 ]; then exit
grep $SrchArg $InFile > $OutFile
echo "Normal end of job."
echo "Execution ended."
exit
Execution results in error messages.
Code:
daniel@daniel-desktop:~$ bash /home/daniel/Desktop/LQfiles/dbm520.bin
: command not foundp/LQfiles/dbm520.bin: line 9:
: command not foundp/LQfiles/dbm520.bin: line 13:
riting to /home/daniel/Desktop/LQfiles/dbm520out.txt
Reading from /home/daniel/Desktop/Voters/wordlist2.txt
: command not foundp/LQfiles/dbm520.bin: line 16:
: command not foundp/LQfiles/dbm520.bin: line 17: echo
: command not foundp/LQfiles/dbm520.bin: line 18: echo
Please enter a search string such as DGE
... (or null to quit.)
DGE
': not a valid identifierles/dbm520.bin: line 21: read: `SrchArg
You entered:
The Search Argument length is: 0
/home/daniel/Desktop/LQfiles/dbm520.bin: line 32: syntax error: unexpected end of file
I don't understand these error messages. Please advise.
Daniel B. Martin
Last edited by danielbmartin; 11-06-2012 at 10:08 AM.
Reason: Correct t7po
daniel@daniel-desktop:~$ bash /home/daniel/Desktop/LQfiles/dbm520.bin
: command not foundp/LQfiles/dbm520.bin: line 9:
It looks like the string ": command not found" is written on top of the string "/home/daniel/Desktop/LQfiles/dbm520.bin". That usually indicates carriage returns (\r). Did you write the file in Windows?
Apart from that, you're missing a fi, and you need spaces around the arguments to [.
Indirectly yes. This bash program was created by copying the source code of a rex program which I wrote several years ago, when I still ran a Windows machine. Since that time I bought a new desktop computer, installed Ubuntu, and have been Linux-only ever since.
I changed...
Code:
- the rex say commands to bash echo commands
- the rex file identifiers to linux
- the rex pull command to a linux read command
etc.
Is there an easy way to get rid of unwanted (and unseen) line feed characters? Is it necessary to key in the whole thing from scratch?
I can't say what the exact problem is yet, but let's clean up a few lines:
Code:
echo "Writing to" $OutFile
echo "Reading from" $InFile
echo "You entered:" $SrchArg
echo "The Search Argument length is: " $LSA
You're leaving the variables unquoted. Just extend them to cover the entire expression.
Code:
grep $SrchArg $InFile > $OutFile
No quotes here either. I'm only singling this one out because each argument will need to be separately quoted.
(And yes, I understand that the values will generally be single words in this case. But it's better safe than sorry.)
Code:
if [ $LSA=0 ]; then exit
Bit mistakes here. There's no closing fi, and the equal sign needs to be its own argument. Although it should really be -eq since it's a numeric comparison. Or even better, use:
Code:
if (( LSA == 0 )); then exit ; fi
(( LSA )) || exit # zero evaluates as false in arithmetic tests.
I can't say what the exact problem is yet, but let's clean up a few lines:
Code:
echo "Writing to" $OutFile
echo "Reading from" $InFile
echo "You entered:" $SrchArg
echo "The Search Argument length is: " $LSA
You're leaving the variables unquoted. Just extend them to cover the entire expression.
Code:
grep $SrchArg $InFile > $OutFile
No quotes here either. I'm only singling this one out because each argument will need to be separately quoted.
(And yes, I understand that the values will generally be single words in this case. But it's better safe than sorry.)
Code:
if [ $LSA=0 ]; then exit
Bit mistakes here. There's no closing fi, and the equal sign needs to be its own argument. Although it should really be -eq since it's a numeric comparison. Or even better, use:
Code:
if (( LSA == 0 )); then exit ; fi
(( LSA )) || exit # zero evaluates as false in arithmetic tests.
I followed your suggestions, and hope correctly. Boiled the program down to bare bones. Still getting garbled error messages. This ...
daniel@daniel-desktop:~$ bash /home/daniel/Desktop/LQfiles/dbm520.bin
DGE
': not a valid identifierles/dbm520.bin: line 4: read: `SrchArg
: No such file or directoryVoters/wordlist2.txt
: command not foundp/LQfiles/dbm520.bin: line 6: exit
daniel@daniel-desktop:~$
I imagine then that it really is a CRLF newline problem. They do tend to mess up the output of many commands when they're encountered.
There are many solutions available to handle it, including pre-built commands like dos2unix, tofrodos, and flip.
But in general any command that removes the non-printing "\r" characters from the text should do.
Code:
tr -d '\r' <dosfile.txt
sed 's/\r$//' dosfile.txt
The sed version removes them only at the ends of lines, so it's a tad safer. Not that you're often going to find carriage returns elsewhere in the text that you want to preserve, however.
It's also possible that there could be other non-printing characters in one of the files as well, such as a byte order mark, or backspaces. Use cat -A to view everything it contains, and/or try creating a completely new file from scratch and manually copy over the visible text into it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.