Unix & Linux Asked by PoweredbyGsus on November 11, 2021
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?
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.
Answered by roaima on November 11, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP