LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to put custom text on Terminal (bash script) ? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-put-custom-text-on-terminal-bash-script-783162/)

lupusarcanus 01-19-2010 02:59 AM

How to put custom text on Terminal (bash script) ?
 
This code:

Code:

#! /bin/bash

echo Okay.
echo

Does not do it....

P.S. I want to show my parents what happens when I do:
Code:

sudo make\ me\ a\ sandwich
and have it say "Okay." :)

(MrCode's profile sig)

:)

lupusarcanus 01-19-2010 03:24 AM

Well, I just read "man printf" and tried it.

Code:

#! /bin/bash

printf "Okay."

It failed. How do i do this?

evo2 01-19-2010 03:34 AM

More info needed!

What do you mean by failed?

Are you getting any error message?

How are you trying to run it?

Did you make the file executable?

Code:

chmod +x myscript
./myscript

Evo2.

lupusarcanus 01-19-2010 03:44 AM

Quote:

Originally Posted by evo2 (Post 3831883)
More info needed!

What do you mean by failed?

Are you getting any error message?

How are you trying to run it?

Did you make the file executable?

Code:

chmod +x myscript
./myscript

Evo2.

Here is my goal:
Code:

$ sudo make\ me\ a\ sandwich
[sudo] password for user:
Okay.

I made a file called "make me a sandwich" and put in:
Code:

#! /bin/bash

echo "Okay."

and saved it to my /home/user directory so I don't have to "cd" into anything to make it work.
Then i made it executable:
Code:

chmod u+x /home/user/make\ me\ a\ sandwich
When I executed:
Code:

sudo make\ me\ a\ sandwich
nothing happened.

I also tried the "printf" instead of "echo" and it did not work.
This is what happens:
Code:

user@box~$ sudo make\ me\ a\ sandwich
user@box~$

What is the correct syntax of echo or printf to make it work?

Thanks and sorry, man pages didn't help much

evo2 01-19-2010 03:49 AM

Try breaking this down into its component parts.

Examples:
- What happens if you don't use sudo?
- What happens if you use a file name with no spaces in it?

You should be able to work this out.

Evo2.

lupusarcanus 01-19-2010 04:00 AM

nevermind, I had to define the full file path, even though I was in the directory where the file is...

Code:

user@box:~$ ~/make\ me\ a\ sandwich
Okay.

Well, It made me my sandwich, but it looks really ugly.

How can I make it so all I have to type is:
Code:

make me a sandwich

or

make_me_a_sandwich

instead of the ~/make_me_a_sandwich?

bigrigdriver 01-19-2010 04:13 AM

I noticed this about your script:
Code:

#! /bin/bash
There should not be a space between #! and /bin/bash. It should be #!/bin/bash.

The ./ (dot slash) tells bash "in this directory". You need it if the directory in question is not in your PATH environment variable. If you add your home directory to your PATH, you won't need the ./ Then, because of the spaces in the file name, just add quotes around the file name: "make me a sandwich"

PATH=$PATH: /home/<username>, where <username> is your user name.

evo2 01-19-2010 04:19 AM

Quote:

Originally Posted by bigrigdriver (Post 3831925)
There should not be a space between #! and /bin/bash. It should be #!/bin/bash.

Actually, IIRC some BSDs require the space, while Linux is indifferent to it. For that reason many people reccommed adding the space to ease portability.

Evo2.

evo2 01-19-2010 04:31 AM

Can't believe I did this...

Code:

.SILENT:
all:
        echo "Make what?"
me_a_sandwich:
        echo "Okay."

Put that in a file called Makefile. Be sure to use a tab to indent.

Then
Code:

make me_a_sandwich
Evo2.

brianL 01-19-2010 05:52 AM

If you're old enough to be writing bash scripts, you're old enough to make your own sandwiches. :)

onebuck 01-19-2010 06:58 AM

Hi,

Unless you include 'Please'.

:hattip:

lupusarcanus 01-19-2010 07:29 AM

everything works. very nice!
Lets see Windows make me a sandwich!


All times are GMT -5. The time now is 10:42 PM.