Open main menu

CDOT Wiki β

Changes

OPS245 Scripting Exercises dev

1,046 bytes added, 21:35, 10 January 2023
no edit summary
A shell script is nothing more than a sequence of shell commands. Any command you put in a shell script can be executed just as well in a terminal. In fact no matter how complex your script is - you can run the entire thing from a terminal window without executing the script. Some of the earliest scripts people learn to create are the commands in the same sequence they would issue them from the command line, only in a script with the appropriate shebang line at the top. You learned about shebang lines in ULI101. The '''Bash''' shebang line is '''#!/bin/bash'''. The '''Python 3''' shebang line is '''#!/usr/bin/env python3'''. The shebang line is the path to the interpreter, and must be the first line of your script.
== Runnning scripts == === Running scripts from your current directory or another directory or a directory in the $PATH ===You can run a script from your current directory with ./ followed by the script name (without a space). I.E. '''./script.bash''' or '''./script.py''' Alternatively, if the script is in a directory that is specified in your $PATH environment variable you can execute the script by simply typing the name of the script without the ./. You can view your $PATH variable by issuing the command '''echo $PATH'''. === Script Permissions ===You need to make sure scripts have the execute permission for the user or groups you wish to be able to execute your script. You can add the execute permission with the '''chmod''' command, which you learned in ULI101. As a quick refresher, what is the difference between the following commands? * chmod +x script.bash* chmod u+x script.bash Both of the above commands will work. However, the first one gives execute permission to user, groups and other (in otherwords '''everyone'''). This is not the most secure way of allowing your scripts to be executed. The second one is a much better practice, which will give only your user execute permission. 
* How to run a command in the current directory or another directory or a directory in the $PATH
* That programs you run need to have execute permission
* What your $PWD is, pwd command
* Check the return code from a command by examining $?