I can't get the read command to work.

Asked by Gerald Newill

jerry@ubuntu:~$ cat > kb_read
#
# kb_read
# A sample program to show the read command
#
echo
echo "Enter your name:\c"
read name
echo "Your name is $name"
echo
jerry@ubuntu:~$ g++ kb_read -o test
kb_read: file not recognized: File format not recognized
collect2: ld returned 1 exit status
jerry@ubuntu:~$

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu gnome-terminal Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Gerald Newill (newill0621) said :
#1

I am extremely new to this and just took the UNIX Operating System class in the college I attend. I got decent grades, but really think that the professor just gave grades away. I really want to learn this system, but am extremely frustrated. Please help.

Revision history for this message
Emre AYTAÇ (eaytac) said :
#2

This program is like a bash script, not a c or c++ program. If you want to see the result, just write:

sh kb_read

Hope this helps...

Revision history for this message
Emre AYTAÇ (eaytac) said :
#3

Or you can give an executable flag this file then run it:

chmod +x kb_read
./kb_read

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#4

You need to have the 1st line read:

#!/bin/bash

I just tested the code and got success with this:

[andy@fileserver ~]$ cat ./test
#!/bin/bash
echo
echo "Enter your name: "
read name
echo "Your name is $name"
echo
[andy@fileserver ~]$ ./test

Enter your name:
andy
Your name is andy

You don't need to run g++ with the code, it will be interpretted by the shell as it is a script, just mark the file executable and it will run. You do this with:

chmod +x kb_read

so the g++ command you are issuing is NOT needed. If the code was a C++ source code file and you wanted to make a linux binary of the code, THEN you would need the g++ command, however in this case it is not

Revision history for this message
Gerald Newill (newill0621) said :
#5

I want to thank all of you for your help. I really do want to learn this system. I did not have much luck getting my professor to respond to issues that we were all having in the class. Believe me, I am still very confused, and can tell you that I will have plenty more questions. I guess that one of the problems that I am having is the fact that the book that we had for the class is not very informative. If any of you can turn me towards some good learning material, I would very much appreciate it. I would like to learn C++ and visual basic unless you all have something better. Thank you again for the help with this issue and with all that I will be asking in the future.
Jerry

Revision history for this message
Gerald Newill (newill0621) said :
#6

I want to thank all of you for your help. I really do want to learn this system. I did not have much luck getting my professor to respond to issues that we were all having in the class. Believe me, I am still very confused, and can tell you that I will have plenty more questions. I guess that one of the problems that I am having is the fact that the book that we had for the class is not very informative. If any of you can turn me towards some good learning material, I would very much appreciate it. I would like to learn C++ and visual basic unless you all have something better. Thank you again for the help with this issue and with all that I will be asking in the future.
Jerry

Revision history for this message
Gerald Newill (newill0621) said :
#7

I am just curious. How do you figure out how to execute or run the programs that you write? The book that I have from the school has us creating files using "cat >" with the name of the file. As well, up until I downloaded UBUNTU, the book taught that the execute command was "a.exe", or "a.out". I am seeing that there are many ways to execute a program, depending on how it is written. Any and all help is appreciated.

Revision history for this message
actionparsnip (andrew-woodhead666) said :
#8

If you run:

nano filename

you will open a new file called filename (change the filename to something else if you wish

nano test_script

is an example of this)

the commands and code you are typing is shell script, not C++ which is why the C compiler is failing. If you mark a text file as executable, it will be a script but you must have the topmost line as:

#!/bin/bash

So that it is read correctly. I suggest you read this:
http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html

Can you help with this problem?

Provide an answer of your own, or ask Gerald Newill for more information if necessary.

To post a message you must log in.