第二章习题ex2.2.sh

习题的问题:


Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting

Prev Chapter 2. Starting Off With a Sha-Bang Next

2.2. Preliminary Exercises

   1.

      System administrators often write scripts to automate common tasks. Give several instances where such scripts would be useful.

   2.

      Write a script that upon invocation shows the time and date, lists all logged-in users, and gives the system uptime. The script then saves this information to a logfile.

Prev Home Next

Starting Off With a Sha-Bang Up Basics

我的答案:

第一题:

backup database; restart web service; backup files; etc..

第二题:

#!/bin/bash

#exercise 2.2

#show date and time

echo "The date and time is: "

date

echo "lists all logged-in users: "

who

echo "the uptime is: "

uptime

echo "write the information above into file ./ex2.2.out"

#if the file ./ex2.2.out exists, delete it at first.

#to do.

echo "The date and time is: " >>  ./ex2.2.out

date >> ./ex2.2.out

echo "lists all logged-in users: " >>  ./ex2.2.out

who  >> ./ex2.2.out

echo "the uptime is: " >>  ./ex2.2.out

uptime >> ./ex2.2.out

echo "finished the work,exit."

exit 0