LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-26-2006, 06:57 PM   #1
nickkrit
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Rep: Reputation: 0
Bash error "Command Not Found"


I just started a course on Linux through University of Phoenix and their textbook resources are kinda weak, so I'm hoping someone here can help. I've looked around online for a couple of days now and cannot figure this one out.

I'm supposed to create a Bash script that asks you your name, you type it in, and then I compare it to my name and output a response.

I've created a file called Week2.sh and it contains this code:
#!/bin/bash
echo "Hello there, what is your first name?"
read firstname;
if ["$firstname"="Nick"] ;
then echo "Wow! What a coincidence, that's my name too."
else
echo "Hey, $firstname is a neat name too."
fi

When I run it fron the command "bash ./week2.sh", the output I get is this:
root@1[linux]# bash ./week2.sh
Hello there, what is your first name?
Nick
: command not found
./week2.sh: line 4: [Nick=Nick]: command not found
: command not found

Any help or advice you can give on what I'm doing wrong here would be appreciated.

Thanks,
Nick
 
Old 07-26-2006, 06:59 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
You don't need the semicolons in the if and read lines. Also, you MUST have a space on each side of the brackets for testing.

Also, welcome to LinuxQuestions.org! I hope you'll find us very friendly and helpful.
 
Old 07-26-2006, 07:04 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Nothing too serious, just need spaces in the brackets.
Code:
#!/bin/bash

#echo "Hello there, what is your first name?"
#read firstname;
read -p "Hello there, what is your first name? " firstname

if [ $firstname = "Nick" ] ; then
   echo "Wow! What a coincidence, that's my name too."
else
   echo "Hey, $firstname is a neat name too."
fi
./test
Hello there, what is your first name? Nick
Wow! What a coincidence, that's my name too.
 
Old 07-26-2006, 07:23 PM   #4
nickkrit
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Matir and Homey,

Thank you both for the quick replies. Unfortunately, when I try your suggestions, it still doesn't work for me, and produces more errors.

When I removed the ";" as you suggested, I got two new errors:

': not a valid identifier 'firstname
./week2.sh: line 9: syntax error: unexpected end of file

I put both the ";"'s back and spaced out the items in the brackets and ran it again.

This time the "./week2.sh: line 4: [Nick=Nick]: command not found" error didn't show up, so at least I'm making progress, but I still got 2 ": command not found"'s and no output.
 
Old 07-26-2006, 07:34 PM   #5
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
Are you editing this file directly on the Linux system, or working remotely and uploading from windows by any chance? I've seen errors like these when the Windows line endings get left on scripts uploaded to a linux machine.
 
Old 07-26-2006, 08:02 PM   #6
nickkrit
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Original Poster
Rep: Reputation: 0
I'm editing directly in Knoppix through KWrite and storing the file on a USB drive.
 
Old 07-26-2006, 08:06 PM   #7
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 50
and you're running it using bash?
 
Old 07-26-2006, 08:13 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
Code:
#!/bin/bash
echo "Hello there, what is your first name?"
read firstname;
if [ "$firstname" = "Nick" ] ;
    then echo "Wow! What a coincidence, that's my name too."
else
    echo "Hey, $firstname is a neat name too."
fi
Works for me.
 
Old 07-26-2006, 08:15 PM   #9
nickkrit
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Success!!!!!!

After Matir's last post, I remembered that I had opened the file on a Windows PC to show a friend the code.

I closed down the file I'd been using and created a brand new file through KWrite, manually typed in all the exact same code, and it works now.

Thanks again for the help. I'm sure I'll probably have more questions over the next few weeks as I take this course.
 
Old 07-26-2006, 08:26 PM   #10
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
No problem. We're more than glad to help you out. Again, welcome to LQ and to Linux!
 
Old 07-26-2006, 08:57 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
hello i hope this is not yet too late. but you can also use this script to convert windows text to unix format. it's just simple.

Code:
#!/bin/bash
#

FILE="$1"
TEMP=".tmp"

cat "$FILE" | sed s/\\r//g > "${TEMP}"
cat "${TEMP}" > "$FILE"
rm "${TEMP}"
welcome to lq too
 
Old 07-26-2006, 09:01 PM   #12
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 50
Quote:
Originally Posted by konsolebox
hello i hope this is not yet too late. but you can also use this script to convert windows text to unix format. it's just simple.
how about dos2unix?

Quote:
Originally Posted by konsolebox
Code:
#!/bin/bash
#

FILE="$1"
TEMP=".tmp"

cat "$FILE" | sed s/\\r//g > "${TEMP}"
cat "${TEMP}" > "$FILE"
rm "${TEMP}"
this can be accomplished with
Code:
sed -i 's/\r//g' $file
 
Old 07-26-2006, 09:05 PM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
yes i know about dos2unix. but i don't have on dos2unix and i rarely use those tools. and good sedding there. i'll take note of that sed command. tnx

Edit: what does the backup thing mean in sed option's -i?
 
Old 07-26-2006, 09:16 PM   #14
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 124Reputation: 124
The '-i' option to sed causes it to do the editing on the same file (edit-in-place), just the same as if you opened the file in vi and edited it and saved it back out.
 
Old 07-26-2006, 09:22 PM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
yeah i figured it out. i guess the backup means if i add suffix:
Code:
       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)
 
  


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
"bash: rpm: command not found" errom when trying to install any package sandeepchau123 Linux - General 16 07-06-2008 04:58 AM
executing a loaded module returns "command not found" error neelay1 Linux - Software 1 05-08-2006 03:25 PM
bash: passwd: command not found "HELP" balweg Red Hat 4 05-05-2005 04:47 PM
"bash: run: command not found" ? neuropulse Linux - Newbie 10 04-23-2005 08:58 AM
bash says "command not found" webguru24 Programming 2 01-05-2004 09:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:07 AM.

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