1

How can I enter the contents of a script that when run would display the words "I should have studied" to stdout?

This is my best try:

#!/bin/bash
$ echo "I should have studied" | grep studied

I know that the first statement is necessary for the shell to execute my command. In the second one, I am using echo to print it and then the pipe operator for that command to be redirected to become the input of my other command. I am also using 'grep' to find any lines in that list which contain the string directory and to send those lines to standard output. Am I making some progress?

7
  • 1
    We don't like it here when people ask questions from their CS exams. We also don't like when people refuse to google. tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-2.html Commented Jul 21, 2020 at 18:54
  • 4
    perhaps you should think about the meaning of the text that you are trying to display
    – jsotola
    Commented Jul 21, 2020 at 18:54
  • $ echo "I should have studied" | grep study Commented Jul 21, 2020 at 19:02
  • Sorry I am just a newbie that is trying to get it right, not to cheat on a test. That will never get you anywhere. I am sure you started somewhere too, and I thought that this was a great resource to do that. Commented Jul 21, 2020 at 19:03
  • 1
    Thanks #roaima, I am taking baby steps here. Commented Jul 21, 2020 at 19:36

1 Answer 1

0

If you're typing directly at a shell you can

echo "I should have studied"

To create a script, all you do is put the commands you want to be run into a (text) file and use that file:

Using you preferred UNIX/Linux editor, create the file study. You can use any name you like (including one with a .sh suffix if you prefer). Add these two lines to it

#!/bin/bash
echo "I should have studied"

The first line declares that it's a bash shell script. You don't need to filter the output with grep as the echo command does enough all by itself. (Also notice you don't type the leading $; some people use this on StackExchange and other sites to show they're typing a command but personally I think it's confusing.) Now make the file executable so it can run like any other program on the system

chmod +x study

And run it

./study

The leading ./ tells the shell to look for the command in the current directory. If the script was in a system directory or otherwise in your search path ($PATH) you would be able just to type its name, study to run it - just like any other command.

For syntax checking, don't be afraid to use https://shellcheck.net/ as and when your scripts start to develop.

1
  • That helps a lot! I will start working on it right away. I so appreciate you providing such an educational experience in kindness. Thanks for helping to change my opinion on this website. After my initial responses, I was discouraged and made me feel like no using this resource ever again. Commented Jul 21, 2020 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .