You can access shell scripts through the file manager and direct them to act upon selected files. To do this, you select files in the file manager and then right-click them to produce a list of shell scripts you've written. From this list, you then choose the scripts that you want to act upon the selected files. In the following sections, again you might need to refer to Chapter 25 if you have trouble following along with the sample scripts. Creating and Using File Manager Shell ScriptsTo create shell scripts that can be used with the file manager (which is responsible for your desktop and the application windows used to copy, open, or move files), create and save any scripts that you want to the ~/.gnome2/nautilus-scripts directory. When a script is called from the file manager, the script behaves just as if it had been called from the command line. The file manager passes the names of the selected files to the script as arguments that can then be accessed through the usual shell variables$1 for the first argument, $2 for the second argument, and so on, or $@ to access the entire list of arguments at once. Sample File Manager Shell ScriptTo illustrate the use of shell scripts with the file manager, let's consider a sample script. Suppose you want to be able to create a backup copy of any file simply by right-clicking the file and selecting a menu option called FileBackup. This menu option would have the effect of creating a copy of the file, with the extension .bak added, in the ~/Backups directory. Enter the script in Listing 26.3 into a text editor now and save it to ~/.gnome2/nautilus-scripts. Then let's go through it line by line.
Listing 26.3. Sample FileBackup Script for Use with Nautilus1: #!/bin/sh 2: 3: if [ ! -d ~/Backups ]; then 4: mkdir ~/Backups 5: fi 6: 7: for BACKUPFILE in $@; do 8: cp $BACKUPFILE ~/Backups/$BACKUPFILE.bak 9: done This script is reasonably short and reasonably simple as well, but it performs as expected. Here's how it works:
After you save the script and exit your editor, don't forget to make the script file executable: [you@workstation20 ~]$ chmod u+x ~/.gnome2/nautilus-scripts/FileBackup To test the script, right-click a file in the file manager window and select the Scripts menu. You see the new FileBackup script in the menu, as shown in Figure 26.9. Figure 26.9. The new FileBackup script appears in the Scripts menu after it has been marked as executable.If you select FileBackup from the Scripts menu, the file you right-clicked is copied with a .bak extension to the ~/Backups folder (this folder is created if it doesn't already exist). Using Nautilus scripts of this sort, you can automate many types of file management functionality from the Linux desktop.
|