Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A basic command and diagnostics shell script UNIX
I wrote a shell script at work to automate some of our Zenworks imaging solutions, since it wasn't very intuitive on how you interact with them. Since then, I have been making more scripts just to get some practice in. Here's one example.

I wrote a basic script that will allow users to run some Terminal commands that may help them with their problems. For instnace, if you are trying to get help on the forums here or elsewhere, you can run the script to help those trying to help you. It will display a monthly calendar, the number of logged in users, and then a menu of commands to run; just enter the number of the item you'd like to run and press Return. Enter 0 and Return to exit. Here's the code:

#!/bin/bash

#This is a menu for basic diagnostics by Thomas Larkin

echo "Hello $USER, Welcome to the Tom's Diagnostic script!"
echo "Today is  ";date
echo "Number of user logged in : " ; who | wc -l
echo "Calendar"
cal

selection=
until [ "$selection" = "0" ]; do
    echo ""
    echo "Select an option please"
    echo "1 - Display all objects in /Volumes"
    echo "2 - Display total disk usage of /, this may take a while.  You will be prompted for admin access"
    echo "3 - Display the disk usage of my Home Directory, this may take a while"
    echo "4 - Print the contents of /var/log/system.log"
    echo "5 - List all current users logged in this computer"
    echo "6 - Display my Network Settings and Information"
    echo "7 - Display my BASH command paths"	
    echo "8 - Display all the current running processes"
    echo "9 - Display current resources being used"	
    echo "10 - Display the print error log"
    echo "11 - Display the crash reporter log"
    echo "12 - Run Verify permissions on the boot volume"	
    echo "13 - Run Repair Permissions on the boot volume"
    echo "14 - Run verify volume on the boot volume"
    echo "15 - list all information of boot volume"
    echo "0 - exit program"
    echo ""
    echo -n "Enter selection: "
    read selection
    echo ""
    case $selection in
       1 ) ls -al /Volumes ;;
       2 ) sudo du -h / | sort ;;
       3 ) du -a -h /Users/$USER | sort ;;
	   4 ) cat /var/log/system.log ;;
	   5 ) finger -h ;;
	   6 ) ifconfig ;;
	   7 ) echo $PATH ;;
	   8 ) ps -A ;;
	   9 ) top -s5 20 ;;
       10 ) cat /var/log/cups/error_log ;;
       11 ) cat /var/log/crashreporter.log ;;
       12 ) diskutil verifyPermissions / ;;
       13 ) diskutil repairPermissions / ;;
       14 ) diskutil verifyVolume / ;;
       15 ) diskutil list / ;;
	   0 ) exit ;;
       * ) echo "Please enter a valid option"
    esac
done

[robg adds: I tested this hint, and it works as described. Copy and paste the text into your favorite pure text editor (or any Terminal text editor, such as vi or pico. Save it, then open Terminal and cd to the spot where you saved it. Make it executable via chmod a+x scriptname.]
    •    
  • Currently 3.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[12,444 views]  

A basic command and diagnostics shell script | 4 comments | Create New Account
Click here to return to the 'A basic command and diagnostics shell script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A basic command and diagnostics shell script
Authored by: szedula on Sep 05, '07 06:02:00PM

I needed make a fix to get thie to run. To fix this problem:

cat: /var/log/system.log: Permission denied

This:

4 ) cat /var/log/system.log ;;

needed to be changed to:

4 ) sudo cat /var/log/system.log ;;



[ Reply to This | # ]
A basic command and diagnostics shell script
Authored by: demmons65 on Sep 06, '07 06:04:14AM
I was able to run the script as advertised, at least after chmod'ing it. Are you running it from an admin account? I am but not as root. Strange.

---
d a v e

http://www.hostwerks.com/~dave/

[ Reply to This | # ]

A basic command and diagnostics shell script
Authored by: szedula on Sep 06, '07 01:20:53PM

On my system : ls -ld /var/log/system.log

produces:

-rw-r----- 1 root admin 53644 Sep 6 12:10 /var/log/system.log

my account is not in the "admin" group so I guess that's why I can't read the file without "sudo".



[ Reply to This | # ]
No need for a text editor
Authored by: kenahoo on Sep 05, '07 07:53:00PM
Instead of this whole dance:
Copy and paste the text into your favorite pure text editor (or any Terminal text editor, such as vi or pico. Save it, then open Terminal and cd to the spot where you saved it.
You can just do:
Copy the text, open Terminal and cd to the spot you want to save it, then do pbpaste > script.sh.
The pbpaste command and its counterpart pbcopy are extraordinarily useful when switching context between the GUI and Terminal - the hardest part is remembering they exist. =)

[ Reply to This | # ]