LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2012, 09:47 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
bash -- command not found


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
 
Old 11-06-2012, 10:12 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
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 [.
 
2 members found this post helpful.
Old 11-06-2012, 10:40 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by ntubski View Post
Did you write the file in Windows?
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?

Daniel B. Martin
 
Old 11-06-2012, 11:10 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 11-06-2012, 12:25 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by danielbmartin View Post
Is there an easy way to get rid of unwanted (and unseen) line feed characters?
Yes, dos2unix.
 
Old 11-06-2012, 03:59 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by David the H. View Post
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 ...
Code:
#!/bin/bash
 InFile="/home/daniel/Desktop/Voters/wordlist2.txt"
OutFile="/home/daniel/Desktop/LQfiles/dbm520out.txt"
read SrchArg
grep "$SrchArg" "$InFile" > "$OutFile"
exit
... produces this ...
Code:
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:~$
The DGE was keyed in by me.

Daniel B. Martin
 
Old 11-07-2012, 01:17 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 11-07-2012, 02:23 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by David the H. View Post
I imagine then that it really is a CRLF newline problem.
Bingo! You are right! This ...
Quote:
Use cat -A to view everything it contains ...
... showed unwanted (and unseen) ^M line feeds, and this ...
Code:
sed 's/\r$//' dosfile.txt
... got rid of them. Now, all is well. Thank you for unravelling this mystery. SOLVED!

Daniel B. Martin
 
  


Reply

Tags
bsh



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
bash: ns: command not found using ns-2.31 Vixxx Linux - Software 25 12-12-2018 01:39 PM
[SOLVED] bash: command not found rohit0825 Linux - Newbie 4 01-24-2010 05:31 PM
bash: vi:command not found shankars02 Linux - General 12 06-09-2009 03:57 PM
bash: rpm: command not found && sudo: alien: command not found Java_Code Ubuntu 7 07-27-2006 11:57 PM
bash: pg: command not found Chomper Programming 2 02-05-2005 02:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 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
Open Source Consulting | Domain Registration