Writing Your First Shell Script
Goal
You will go around the skeleton of a shell script once: shebang, execute permission, arguments, environment variables, conditional branches, and exit codes.
Why it matters
Scripts are called not by people but by other programs. So the output streams and the exit code are the interface. Results go to stdout, errors and usage go to stderr, and success or failure goes in the exit code. Only when you keep this convention does a usage such as result=$(script.sh) work and can CI detect failures. The moment you send the usage message to stdout, it becomes the result value. And passing configuration through environment variables and running with a reasonable default when they are absent is the standard form in deployment automation.
Steps
- Create
/root/bin/greet.shand make its first line#!/bin/bash. - Give that file execute permission.
- Make
greet.sh labhubprint exactlyHello, labhub!. - Add a default so that running it without arguments gives
Hello, world!. If an argument is given, that argument must still be used. - If the environment variable
GREETINGis set, change the greeting to its value.GREETING=Hi greet.sh labhubmust giveHi, labhub!, with no setting it must beHello, andGREETING=Yo greet.shmust giveYo, world!. - Create
/root/bin/checkfile.sh <경로>(the argument is a path). It printsfilefor a regular file,dirfor a directory, andmissingif it does not exist. - Create
/root/bin/grade.sh <점수>(the argument is a score). 90 or above isA, 80–89 isB, 70–79 isC, and anything below that isF. - Create
/root/bin/deploy.sh. It reads the environment variableAPP_ENVand:- if it is not set, prints a message to standard error and exits with exit code
2 - if the value is neither
prodnorstage, exits with exit code3 - if it is
prodorstage, printsdeploying to <값>(the value goes in place of the placeholder) and exits with exit code0
- if it is not set, prints a message to standard error and exits with exit code
Notes
${1:-world}uses world only when the first argument is missing or empty.- Write to standard error with
echo "메시지" >&2. - Common mistake 1: if you overwrite the positional argument while adding the environment variable default in step 5, step 4 breaks. The two must work independently.
- Common mistake 2: if you write
>=as>in step 7, 90 falls to B. Try each boundary value by hand.
Script file and shebang
Create /root/bin/greet.sh and make its first line #!/bin/bash.
The #! at the start of the first line is a marker that tells the kernel which interpreter to use to run this file.
Give it execute permission
Give that file execute permission.
Turn on the execute bit with chmod +x.
Take an argument and print it
Make greet.sh labhub print exactly Hello, labhub!.
The first positional argument is $1. You must match the exact placement of the comma and the exclamation mark.
Default value for an argument
Add a default so that running it without arguments gives Hello, world!. If an argument is given, that argument must still be used.
With the ${1:-default} syntax, the default is used only when there is no argument. If there is an argument, it must stay as it is.
Change the greeting with an environment variable
If the environment variable GREETING is set, change the greeting to its value. GREETING=Hi greet.sh labhub must give Hi, labhub!, with no setting it must be Hello, and GREETING=Yo greet.sh must give Yo, world!.
Both the environment variable and the positional argument need default handling. They must not interfere with each other.
Identify the file type
Create /root/bin/checkfile.sh <경로> (the argument is a path). It prints file for a regular file, dir for a directory, and missing if it does not exist.
-f and -d test for a regular file and a directory respectively. If you get the order wrong, a directory may fall through to missing.
Assign a grade to a score
Create /root/bin/grade.sh <점수> (the argument is a score). 90 or above is A, 80–89 is B, 70–79 is C, and anything below that is F.
The boundary values are the trap. Check 90 and 89, 80 and 79, and 70 and 69 by hand.
Environment variable validation and exit codes
Create /root/bin/deploy.sh. It reads the environment variable APP_ENV and:
- if it is not set, prints a message to standard error and exits with exit code
2 - if the value is neither
prodnorstage, exits with exit code3 - if it is
prodorstage, printsdeploying to <값>(the value goes in place of the placeholder) and exits with exit code0
You must distinguish standard output from standard error and return a different exit code for each situation. The number after exit is the convention.