1.3 KiB
1.3 KiB
Linux+ 4.2.1: Shell script basics
A script with arguments, a default value and exit codes, and parameter expansion.
Before you start
Work as root, so ~ is /root. The lab only checks that ~/info.sh
doesn't exist yet. Write answers to the files in /root/lab-answers/.
Tasks
- Write the executable script
~/info.sh: withset -u, it takes a host as its first argument (default: this machine's hostname), printsChecking HOST (N args, script NAME, PID PID), then pings the host once with a one-second timeout and printsupand exits 0, or printsdownand exits 1. Run it with no argument, with192.0.2.1, and withbash -xandlocalhost. - Set
f=/var/log/nginx/access.logand, with parameter expansion only (no external commands), print on one line: the file name, the directory, the path with.logreplaced by.old, the length of the path, and the path in capitals, separated by spaces. Save that line in/root/lab-answers/2.
Hints
#!/bin/bash,${1:-$(hostname)},$#,$0,$$,if ping -c1 -W1 "$host" >/dev/null 2>&1; then ...,chmod +x,echo "exit $?".${f##*/},${f%/*},${f%.log},${#f},${f^^}.- The chapter's step 2 (export and subshells) isn't graded; try it anyway.