a script, so that we can re-run that script as much as we'd like. So hello.sh is not very impressive, but it captures the gist of what we want to do: wrap up a series of commands into a file, i.e. Here's what the steps look like as a GIF: Run the hello.sh script with this command: bash hello.sh nano will then confirm if you want to save to the file named hello.sh.nano will ask you if you want to save the modified file.Then press Ctrl-X on your keyboard to Exit nano.Type in the shell command: echo "Hello world" Nano should open up and present an empty file for you to work in. ![]() Let's use the nano text editor to create a shell script named hello.sh. Make a junk directory somewhere, such as /tmp/my-playground and change into it – we don't need to litter our actual workspace with test code.Ī shell script is nothing more than a text file, which should make sense, as all of our command-line scripting has so far been, well, text. So for complex tasks that we want to repeat, the best practice is not to retype their code from the beginning, but to create a self-contained shell script that can be run as a one-liner. However, as you've likely noticed, typing is a very error-prone activity. ![]() Using the command-line interface in this interactive fashion is fine, when trying things out. Here is a GIF of me using the command line to explore a directory of Shakespearean plays, counting the number of words and the number of times the word "murder" appears, both in King Lear and in all of Shakespeare's plays: That is, we enter one command at a time so that we can see the results of each command. When we start to learn the command-line interface, we generally learn and explore it interactively. ![]() How to wrap your code into a file and run it from the command-line
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |