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:


#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"

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"

[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.]
  Post a comment  •  Comments (4)  
  • Currently 2.80 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
[12,768 views]  View Printable Version
Advanced readline settings for bash in Terminal UNIX
The bash shell used in the Terminal application can be tweaked a little for better interactive experience, by saving the following lines as .intputrc in your home directory:
set editing-mode vi
set show-all-if-ambiguous on
set completion-ignore-case on
set meta-flag on
set convert-meta off
set output-meta on
set bell-style visible
To be more precise, the .inputrc file changes some default settings for the readline library, which the bash shell uses when being used in interactive mode. For more options, see the readline man page. If you are more used to the emacs keybindings to edit the command line, omit the editing-mode line in the .inputrc file.

[robg adds: We've run a number of these before in separate hints, but there are a couple of new ones in this list. For a list of all the possible variables and an explanation of what each controls, type man bash, then press the forward-slash (/) to search, and type Readline Variables as the search term (capitalization counts).]
  Post a comment  •  Comments (8)  
  • Currently 2.80 / 5
  You rated: 3 / 5 (5 votes cast)
[19,937 views]  View Printable Version
Use a free program to simplify dragging from Terminal UNIX
For a long time I've really liked that you can drag files from the finder (or elsewhere) to a Terminal window, where they appear as a path. Unfortunately, there has never been a nice way to drag from the Terminal. For example, if I have a new tarball that I want to send someone, I have to click Attach in Mail, and then find the file using the Open sheet.

I've just written a program -- -- that does allow you to drag from Terminal, and it is available for free, with source. It adds a new command (shell script), drag , which is usable from Terminal. When executed, it opens a new window with a file icon that can be dragged as normal. When the drag is complete, the app quits so your desktop isn't cluttered and you can get back to the command line.

The closest I've seen to this before is the open command. If you open the enclosing directory of a file, then it will be displayed in the Finder -- you can find and drag a file from there. That approach has the downside that you'll sometimes get a .DS_Store file, and it can still be hard to find the file you want if the directory has a lot of files in it. My command cuts to the chase and lets you drag the file(s) you want, then gets out of your way.

[robg adds: I tried this, and it worked well. The download includes a compiled Intel Mac version; you'll have to compile your own for PowerPC, and that's left as an exercise to the reader...]
  Post a comment  •  Comments (27)  
  • Currently 3.20 / 5
  You rated: 4 / 5 (5 votes cast)
[7,589 views]  View Printable Version
Batch rename image files sequentially via perl script UNIX
I wanted to use the QuickTime "Image Sequence" feature to make rapid MPG movie out of a folder of JPGs. The problem was there were 600 pictures taken with my iSight over seven months, so I used a simple perl script to bulk-rename the files (it actually uses cp, so your original files are untouched). After running the script, open QuickTime, choose File » Open Image Sequence, and select 1.jpg, then the frame rate, etc. Here's an example of the finished product. (I used the iSight auto-capture hint I submitted earlier this year.)

Here is the perl (


foreach my $file (`ls *.jpg`) {
    system("cp $file $iteration.jpg;");
[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (12)  
  • Currently 3.80 / 5
  You rated: 5 / 5 (5 votes cast)
[19,103 views]  View Printable Version
Avoid duplicates when copying music from another PC UNIX
I am sure some of you have had this issue before if you use more than one platform at home. Personally for me, OS X and iTunes lack some audio features I like so I don't do my audio ripping from my personal CD collection on a Mac. I do it on my windows/Linux box so I can take advantage of some good open source formats like OGG, FLAC, and MP3vbr (though I think the newest release of itunes will support VBR? not sure). [Editor: iTunes has had VBR support for quite a while.] However, I like to have my music collection on my Mac so I can listen to my music when I am working on it, and my laptop is a Macbook Pro.

I am sure a lot of you already know this, and this is nothing new or exciting, but I have used basic Unix commands to copy my music directory from my PC to my Mac using rsync, and have not had it copy over any duplicate albums. For example, I have about 40GB of music ripped to my PC of my own personal collection. When I copy it via SMB over the network, it duplicates about every five albums or so which is annoying. To get around this I simply used rsync instead. Make sure all the proper permissions are in place, so you can mount your share from Windows or Linux box on your mac. Connect to the share and mount it. Once it's mounted, use this very simple command:
rsync -r -u /path/to/your/music /destination/on/your/mac
You can use this over and over again -- the -u tag should only copy new material from your other computer and not copy what has already been copied. I have been using this method for about a month now, since I rip everything in FLAC then convert it to MP3 and then copy it to my Mac via Terminal. So far no more duplicate albums are being copied using this method.

Also since you can simply drag and drop the file paths directly into Terminal, it makes it even easier. If you were so inclined, you could make a shell script doing this with a cron job so it auto-updates once a week or whatever. This method could be applied from Mac to Mac as well, but if your Macs are networked, you could just share it over AirTunes or what not.
  Post a comment  •  Comments (5)  
  • Currently 3.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
[12,279 views]  View Printable Version
Add high-end printers to other machines via the shell UNIX
This excellent tip explained how to manage LPR printers using the command line lpadmin printing tool, but there were no instructions on how use printer queues for high-end printers with a RIP -- like those from Xerox and Canon. Typically, these printer queues will contain options such as Print, Hold or Direct. Although it's easiest to use Open Directory to manage your printers and these special settings, if you use ARD or Filewave for Desktop management, you can issue a shell command or shell script to automatically add these special type of printers without end-user disruption.

Simply add your printer's queue to the end of the IP address or URL like the example shell script below, and voila, your users will have all of their special printer configurations added automagically.

#This script installs the left and right Xerox printers in Imaging

lpadmin -p Left242 -L "Left Xerox 242" -E -v lpd:// -P "/Library/Printers/PPDs/Contents/Resources/en.lproj/Fiery EX260 Color Server PS.gz"

/System/Library/StartupItems/PrintingServices/PrintingServices restart

[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (1)  
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
[7,767 views]  View Printable Version
Use Skim to view TeX source in TeXShop UNIX
Recently, someone asked me how they could use Skim as his TeXShop previewer. That is, he really liked Skim and wanted to be able to use its PDFSync support to bring up a line of TeX source inside TeXShop. So I put together a shell script that mainly consists of the following:

[ "${fileName:0:1}" == "/" ] || fileName="${PWD}/${fileName}"
[ "${lineNumber}" == "" ] || gotoString="goto line ${lineNumber}"

exec osascript \
  -e "set texFile to POSIX file "${fileName}"" \
  -e "tell application "TeXShop"" \
    -e "activate" \
    -e "open texFile" \
    -e "tell front document" \
      -e "refreshtext" \
      -e "${gotoString}" \
    -e "end tell" \
  -e "end tell"
If you save this script as texshop in your PATH (e.g, as /usr/local/bin/texshop) and chmod it 0755 (e.g., chmod 0755 /usr/local/bin/texshop), then you can go into the Skim LaTeX preferences and set the Preset to Custom, with the Command set to texshop, and the Arguments set to "%file" %line. I hope some other people find this useful

[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (2)  
  • Currently 2.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
[9,312 views]  View Printable Version
Save and merge tcsh history across windows and sessions UNIX
After reading a lot of things about the issue of non-saving history files after session closing in tcsh (my favourite shell, especially for its completion flexibility), I founded that putting these lines...
set history = 2000          # History remembered is 2000
set savehist = (2000 merge) # Save and merge with existing saved 
set histfile = ~/.tcsh_history .tcshrc and this line...
history -S .logout solved the problem. Now history is not only preserved between Terminal sessions, but also merged (considering command dates). Also, it is not necessary to issue an exit command, but with a normal window close, the current history is merged.

[robg adds: This older hint covered savehist, but it seems you still had to use exit to get everything to work. The above solution should also work if you simply close a Terminal window.]
  Post a comment  •  Comments (2)  
  • Currently 3.13 / 5
  You rated: 5 / 5 (8 votes cast)
[13,105 views]  View Printable Version
A more functional command-line calculator UNIX
I have read all the hints about command line calculators, and I've tried just about all of them, including:
  1. Python
  2. awk
  3. bc
Unfortunately, none of these options had built-in functions like sin() or cos(), or constants like pi. For Python, in order to include the math library you have to type from math import *. But then to get it to do the things you want (like adjusting precision), you have to be familiar with Python and/or you have to read documentation on the Python math library. The problem with bc is that when you type bc -l you only get six built-in functions, such as s(), c(), a(), which are sine, cosine, and arctangent respectively. You don't get any built-in constants.

There's a better way, but it requires a small download of a program called wcalc. If you have Fink installed, you can just type fink install wcalc (or get the pre-compiled version via sudo apt-get install wcalc. After you've installed wcalc, on the command-line in Terminal, type wcalc and you're set to go. The program provides you with a prompt and tells you "Enter an expression to evaluate, q to quit, or ? for help." It's pretty self-explanatory.

[robg adds: The Fink version of wcalc is 1.7. However, on the home page I linked above, you can download version 2.2.2 for Mac OS X, which also includes a GUI version.]
  Post a comment  •  Comments (17)  
  • Currently 2.67 / 5
  You rated: 3 / 5 (3 votes cast)
[10,172 views]  View Printable Version
Display latest jobs in Terminal titles (revisited) UNIX
Showing the latest job in each window title is my preferred way to tell multiple Terminals apart. Unfortunately the GUI method is rather useless (File » Set Title... » Active Process Name will make most windows say bash), and this old hint only covered tcsh. To update it, here is (in a nutshell) the command to issue, or to put in the appropriate startup file:
  • In bash 2.05b or newer(default since Panther):
     trap 'printf "\033]0;  `history 1 | cut -b8-`  \007"' DEBUG
  • in ksh ksh93 and newer:
     trap 'printf "\033]0;  `history -0 | cut -b5-`  \007"' DEBUG
  • in tcsh 6.08.03 and newer (default before Panther):
     alias postcmd 'printf "\033]0; `history -h 1` \007"'
  • in zsh 3.0.6 or newer:
     preexec() { printf "\033]0;  `history $HISTCMD | cut -b7-`  \007"; }
While the how-to cited in the old hint did contain the zsh and tcsh methods, and while in retrospect the ksh trap '...' DEBUG method had been working since long before, the idea of using it for the present purpose seems newer and due to Paul Jarc, who first mentioned it shortly after bash switched to the ksh93 standard of running '...' before (rather than after) each interactive command. (Prior to that, the effect was reputedly impossible to obtain without recompiling bash.)

Lately zsh (≥ 4.3.3) also rescheduled the trap, so recent versions also support Jarc's method -- provided you preface it with set -o debugbeforecmd.
read more (204 words)   Post a comment  •  Comments (7)  
  • Currently 1.25 / 5
  You rated: 1 / 5 (4 votes cast)
[14,491 views]  View Printable Version