Easily find issues by searching: #<Issue ID>
Example: #1832
Easily find members by searching in: <username>, <first name> and <last name>.
Example: Search smith, will return results smith and adamsmith
Write a script to carry out each of the following tasks.
Write a script that backs itself up, that is, copies itself to a file named backup.sh
.
Hint: Use the cat command and the appropriate positional parameter.
Perform a recursive directory listing on the user's home directory and save the information to a file. Compress the file, have the script prompt the user to insert a USB flash drive, then press ENTER. Finally, save the file to the flash drive after making certain the flash drive has properly mounted by parsing the output of df. Note that the flash drive must be unmounted before it is removed.
Convert the for loops in Example 11-1 to while loops. Hint: store the data in an array and step through the array elements.
Having already done the "heavy lifting," now convert the loops in the example to until loops.
Write a script that reads each line of a target file, then writes the line back to stdout
, but with an extra blank line following. This has the effect of double-spacing the file.
Include all necessary code to check whether the script gets the necessary command-line argument (a filename), and whether the specified file exists.
When the script runs correctly, modify it to triple-space the target file.
Finally, write a script to remove all blank lines from the target file, single-spacing it.
Write a script that echoes itself to stdout
, but backwards.
Given a list of filenames as input, this script queries each target file (parsing the output of the file command) for the type of compression used on it. Then the script automatically invokes the appropriate decompression command (gunzip
, bunzip2
, unzip
, uncompress
, or whatever). If a target file is not compressed, the script emits a warning message, but takes no other action on that particular file.
Generate a "unique" 6-digit hexadecimal identifier for your computer. Do not use the flawed hostid command. Hint: md5sum /etc/passwd, then select the first 6 digits of output.
Archive as a "tarball" (*.tar.gz
file) all the files in your home directory tree (/home/your-name
) that have been modified in the last 24 hours. Hint: use find.
Optional: you may use this as the basis of a backup script.
Given a process ID (PID) as an argument, this script will check, at user-specified intervals, whether the given process is still running. You may use the ps and sleep commands.
Print (to stdout
) all prime numbers between 60000 and 63000. The output should be nicely formatted in columns (hint: use printf).
One type of lottery involves picking five different numbers, in the range of 1 - 50. Write a script that generates five pseudorandom numbers in this range, with no duplicates. The script will give the option of echoing the numbers to stdout
or saving them to a file, along with the date and time the particular number set was generated. (If your script consistently generates winning lottery numbers, then you can retire on the proceeds and leave shell scripting to those of us who have to work for a living.)
Write a script function that determines if an argument passed to it is an integer or a string. The function will return TRUE (0) if passed an integer, and FALSE (1) if passed a string.
Hint: What does the following expression return when $1 is not an integer?
expr $1 + 0
The atoi function in C converts a string character to an integer. Write a shell script function that performs the same operation. Likewise, write a shell script function that does the inverse, mirroring the C itoa function which converts an integer into an ASCII character.
List, one at a time, all files larger than 100K in the /home/username
directory tree. Give the user the option to delete or compress the file, then proceed to show the next one. Write to a logfile the names of all deleted files and the deletion times.
Simulate the functionality of the deprecated banner command in a script.
Inactive accounts on a network server waste disk space and may become a security risk. Write an administrative script (to be invoked by root
or the cron daemon) that checks for and deletes user accounts that have not been accessed within the last 90 days.
Write a script for a multi-user system that checks users' disk usage. If a user surpasses a preset limit (500 MB, for example) in her /home/username
directory, then the script automatically sends her a "pigout" warning e-mail.
The script will use the du and mail commands. As an option, it will allow setting and enforcing quotas using the quota and setquota commands.
For all logged in users, show their real names and the time and date of their last login.
Hint: use who, lastlog, and parse /etc/passwd.
Implement, as a script, a "safe" delete command, sdel.sh
. Filenames passed as command-line arguments to this script are not deleted, but instead gzipped if not already compressed (use file to check), then moved to a ~/TRASH
directory. Upon invocation, the script checks the ~/TRASH
directory for files older than 48 hours and permanently deletes them. (An better alternative might be to have a second script handle this, periodically invoked by the cron daemon.)
Extra credit : Write the script so it can handle files and directories recursively. This would give it the capability of "safely deleting" entire directory structures.
What is the most efficient way to make change for $1.68, using only coins in common circulations (up to 25c)? It's 6 quarters, 1 dime, a nickel, and three cents.
Given any arbitrary command-line input in dollars and cents ($*.??), calculate the change, using the minimum number of coins. If your home country is not the United States, you may use your local currency units instead. The script will need to parse the command-line input, then change it to multiples of the smallest monetary unit (cents or whatever). Hint: look at Example 24-8.
Solve a quadratic equation of the form Ax^2 + Bx + C = 0. Have a script take as arguments the coefficients, A, B, and C, and return the solutions to five decimal places.
Hint: pipe the coefficients to bc, using the well-known formula, x = ( -B +/- sqrt( B^2 - 4AC ) ) / 2A.
Table of Logarithms
Using the bc and printf commands, print out a nicely-formatted table of eight-place natural logarithms in the interval between 0.00 and 100.00, in steps of .01.
Hint: bc requires the -l option to load the math library.
Unicode Table
Using Example T-1 as a template, write a script that prints to a file a complete Unicode table.
Hint: Use the -e option to echo: echo -e '\uXXXX', where XXXX is the Unicode numerical character designation. This requires version 4.2 or later of Bash.
Sum of Matching Numbers
Find the sum of all five-digit numbers (in the range 10000 - 99999) containing exactly two out of the following set of digits: { 4, 5, 6 }. These may repeat within the same number, and if so, they count once for each occurrence.
Some examples of matching numbers are 42057, 74638, and 89515.
Lucky Numbers
A lucky number is one whose individual digits add up to 7, in successive additions. For example, 62431 is a lucky number (6 + 2 + 4 + 3 + 1 = 16, 1 + 6 = 7). Find all the lucky numbers between 1000 and 10000.
Craps
Borrowing the ASCII graphics from Example A-40, write a script that plays the well-known gambling game of craps. The script will accept bets from one or more players, roll the dice, and keep track of wins and losses, as well as of each player's bankroll.
Tic-tac-toe
Write a script that plays the child's game of tic-tac-toe against a human player. The script will let the human choose whether to take the first move. The script will follow an optimal strategy, and therefore never lose. To simplify matters, you may use ASCII graphics:
o | x |
----------
| x |
----------
| o |
Your move, human (row, column)?
Alphabetizing a String
Alphabetize (in ASCII order) an arbitrary string read from the command-line.
Parsing
Parse /etc/passwd, and output its contents in nice, easy-to-read tabular form.
Logging Logins
Parse /var/log/messages to produce a nicely formatted file of user logins and login times. The script may need to run as root. (Hint: Search for the string "LOGIN.")
Pretty-Printing a Data File
Certain database and spreadsheet packages use save-files with the fields separated by commas, commonly referred to as comma-separated values or CSVs. Other applications often need to parse these files.
Given a data file with comma-separated fields, of the form:
Jones,Bill,235 S. Williams St.,Denver,CO,80221,(303) 244-7989
Smith,Tom,404 Polk Ave.,Los Angeles,CA,90003,(213) 879-5612
...
Reformat the data and print it out to stdout in labeled, evenly-spaced columns.
Justification
Given ASCII text input either from stdin or a file, adjust the word spacing to right-justify each line to a user-specified line-width, then send the output to stdout.
Mailing List
Using the mail command, write a script that manages a simple mailing list. The script automatically e-mails the monthly company newsletter, read from a specified text file, and sends it to all the addresses on the mailing list, which the script reads from another specified file.
Generating Passwords
Generate pseudorandom 8-character passwords, using characters in the ranges [0-9], [A-Z], [a-z]. Each password must contain at least two digits.
Monitoring a User
You suspect that one particular user on the network has been abusing her privileges and possibly attempting to hack the system. Write a script to automatically monitor and log her activities when she's signed on. The log file will save entries for the previous week, and delete those entries more than seven days old.
You may use last, lastlog, and lastcomm to aid your surveillance of the suspected fiend.
Checking for Broken Links
Using lynx with the -traversal option, write a script that checks a Web site for broken links.
About AquaClusters Privacy Policy Support Version - 19.0.2-4 AquaFold, Inc Copyright © 2007-2017