My first shell script

Started by Gardener, November 07, 2014, 01:15:52 PM

Previous topic - Next topic

Gardener

For those here who are unix users, this is probably a "ummm, so what, that's EASY". Yes, it is, but I have very, very little experience in programming. My only other experience was in my intro to OS class where we had to use batch files and it was sooooo painful due to the needless complexity of batch.

I was pretty excited when it ran successfully and my cat filename revealed all the necessary input.

Assignment:
Write a shell script named snapshot that will prompt the user
for a file name and then store the current time, date,
username, currently executing processes that belong to that
user, and contents of the users' home directory in the given
file name.


Script:
Quote
#!/bin/bash
#snapshot: record user-supplied filename, store current time, date and username,
# currently executing processes belonging to user and contents
# of user home directory in given filename
# created by Jon Horton
echo -n "Please specify a filename: "
read filename
file=$HOME/$filename
date >> $file
echo "-----------------" >> $file
echo "USER NAME" >> $file
id -u -n >> $file
echo "-----------------" >> $file
echo "Current Processes" >> $file
ps -u $USER >> $file
echo "-----------------" >> $file
echo "USER HOME DIRECTORY CONTENTS:" >> $file
ls ~ >> $file
echo "-----------------" >> $file
echo "end" >> $file
echo "-----------------" >> $file
exit
"If anyone does not wish to have Mary Immaculate for his Mother, he will not have Christ for his Brother." - St. Maximilian Kolbe

Lynne

In conclusion, I can leave you with no better advice than that given after every sermon by Msgr Vincent Giammarino, who was pastor of St Michael's Church in Atlantic City in the 1950s:

    "My dear good people: Do what you have to do, When you're supposed to do it, The best way you can do it,   For the Love of God. Amen"