IgnorantGuru's Blog

Linux software, news, and tips

Script Installation Instructions

Contents

How To Install A Script


Download the script or archive and save to any location. Remember to authenticate. If you downloaded an archive, extract the contents before proceeding.

Open a terminal window (for example in Gnome select Applications|Choose Accessories|Terminal, or in KDE select K|System|Konsole). Enter the directory where you saved the script file (/home/myuser/downloads/ in this example). Then run the ‘install’ command as root:

cd /home/myuser/downloads
sudo install SCRIPTFILE /usr/local/bin/SCRIPTNAME

where SCRIPTFILE is the script’s filename as you downloaded it, and SCRIPTNAME is the name you want it installed as (the command you want to type to run the script). Note that install must be run as root (using sudo or another method).

For example, if you downloaded sandfox, you would enter:

sudo install sandfox /usr/local/bin/sandfox

This will install “sandfox” to /usr/local/bin. You can now run the script by simply typing “sandfox”:

sandfox --help

IMPORTANT: Some scripts also have required dependencies – install these with your package manager before running the script or it probably won’t work. Also, some scripts are available as Debian/Ubuntu packages, and are also available in the PPA, which automates installation and alerts you to updates.

 

Security Issues


Note that saving and running UNinstalled scripts leaves the scripts vulnerable to modification by other users, malware, etc. It is best to install scripts to /usr/local/bin using the above method because it will improve the security of your system. The script, once installed, will be owned by root in a directory which is writable by root only, like other programs on your system.

“sudo” means ‘run this command as root’. Installing a script as root is different from running it as root. When you run “sudo install” to install a script as root, the script is NOT run as root – it is only copied to the system and protected from tampering. ALL scripts that you plan to use should be installed with “sudo install”.

However, before running the script itself with sudo, be sure it requires root priviledges to do what it does, and use extra caution. Also note that regardless of whether you run a script as a normal user or as root, a malicious or poorly written script can damage your files. See Fear Not Root for more information.

 

How To Remove A Script


To remove a script, simple delete it from its install location. In the example above sandfox was installed to /usr/local/bin. To remove it:

sudo rm /usr/local/bin/sandfox

 

What IS A Script?


A script is a plain text file containing a series of console commands. You can load any script into your text editor to view the contents or make changes. Scripts are read or “run” by an interpreter, such as “bash”. Any command that you enter into a Linux console (such as ls, chmod, apt-get), can be put into a script. Scripts provide a convenient way to run a series of commands. Scripts can also be programmed with basic decision-making capabilities, so they can perform differently in different situations. Scripts are the Linux version of DOS/Windows batch files, only much more powerful. Because Linux allows you to do almost everything from the command line, you can do almost everything with scripts.

Here is a very simple script:

#!/bin/bash

# The following command displays folder contents
ls

The very first line of a script tells Linux what interpreter to use to run the script – in this case bash. (Scripts can be written in different interpreter languages.) The second line above, beginning with “#”, is a comment – such lines are ignored by the bash interpreter; they are only there for you to read. Also, commands in scripts can be disabled by turning them into a comment (add a “#” at the beginning of the line). The third line is simply an ls command. Running this script is equivalent to entering the ls command directly – it will display the current folder’s contents.

Feel free to change and add your commands to scripts. Anything you know how to do with a console command can be thrown into a script. Any task you find yourself doing repetitively can be automated in a script. For a more detailed reference on script programming, I recommend the Advanced Bash-Scripting Guide.

 

How To Setup A Custom Scripts Folder


Scripts can be installed to any location. For most users it’s best to install scripts using the standard method above – this places the scripts in a reliable location which is already in your PATH. The following method is for users who want to install scripts elsewhere, perhaps in a dedicated scripts folder. One reason for doing so is organization; another is to control which users can run which scripts.

The following example installs scripts to the /opt/scripts folder and makes the scripts owned by root. This is in keeping with the Linux convention of making software owned by root and executable by users – this improves the security of your system.

First, set up the /opt/scripts folder (you only need to do this once).
Open a terminal window and paste or enter the following commands:

# Setup custom scripts folder
sudo mkdir -p /opt/scripts
sudo chown root:root /opt/scripts
sudo chmod u+rwx,go+rx-w /opt/scripts

To install a script, use the ‘install’ command or enter these commands:

sudo cp ~/myscript /opt/scripts
sudo chmod +x /opt/scripts/myscript

You can now run the script with this command:

/opt/scripts/myscript

Or, to run the script as the root user (use with caution!):

sudo /opt/scripts/myscript

Setting The PATH

If you don’t want to specify the location of a script when you run it, you can add /opt/scripts to your PATH variable, which specifies where executable files are located. To do so, open the file ~/.bashrc (in your user’s home folder) and add the following line at the very end:

PATH=/opt/scripts:$PATH

Or, you can do so from the console using this command (two lines):

# Add /opt/scripts to current user's path
if [ "`grep '^PATH=/opt/scripts:$PATH' ~/.bashrc`" = "" ]; then \
echo 'PATH=/opt/scripts:$PATH' >> ~/.bashrc; fi

Close your console window, then reopen it. Now you can run your script with the following command:

myscript

It is also possible to add /opt/scripts to root’s PATH by editing /root/.bashrc

To add /opt/scripts to root’s PATH:

# Add /root/bin to root's path
if [ "`sudo grep '^PATH=/opt/scripts:$PATH' /root/.bashrc`" = "" ]; then \
sudo sh -c 'echo "PATH=/opt/scripts:\$PATH" >> /root/.bashrc'; fi

Note: root’s PATH will not normally work through the sudo command. It will only work if the root user has been enabled, or in a shell started by “sudo -i” or “su -“. This means that you may need to specify the full location of a script when running it with sudo, not just the script’s name. To avoid this problem, install the script to /usr/local/bin.

Consult Ubuntu RootSudo for additional information on the sudo command.

6 Comments

  1. Is there a way to reference another tab in a script? I know it’s bash, but I’m hoping to be able to move a file from the current tab to a different tab. Same for copy or link. I don’t like single-pane fm’s drag & drop or cut & paste mentality, I’d rather do the operation in one go.

    Comment by freds_shadow | September 16, 2011

    • Assuming you’re referring to PCManFM-Mod, there is no direct way for a script to access another tab. This is something I’ll keep in mind for any future projects, as I like that ability too, but it doesn’t fit easily into the current design of single-pane PCManFM-Mod.

      One workaround to this would be to have a user command grab the list of files selected on one tab, or the dir of the current tab, save that info somehow (perhaps to a temp file), and then have a user command from another tab make use of that saved info. So this would be a two-step user command that makes use of multiple tabs. For example you could press F4 in one tab to mark that as the current default destination, then press F6 in other tabs to copy files to the that destination. The F4 script could grab the dir from the file list passed to it using the ‘dirname’ command, for example.

      Comment by IgnorantGuru | September 17, 2011

    • To update my above reply, SpaceFM (PCManFM-Mod’s successor) can do the cross-tab scripting. See Design Mode to get started.

      Comment by IgnorantGuru | October 18, 2013

  2. Can a script request user input? I’m thinking of a simple bulk-rename function using wildcards similar to what dos used (* and ?). Worker fm uses a bash script to rename using those patterns, but it requires you to give a file pattern to be matched against, and for anything matching that pattern you give an expected output. *text* as the pattern to match against and pic* as the output changes opentexts.doc to pics.doc.

    I’m expecting scripts act invisibly with no request for input other than chooser’s ‘which of these things shall I do?’

    Comment by freds_shadow | September 16, 2011

    • Using a program like zenity it is very easy for a script to get user input of various kinds, and this works well with PCManFM-Mod. Although less distributed than zenity, there is also yad, which is an extended version of zenity which I’ve heard good things about (still haven’t gotten around to trying it). And there are other similar programs as well.

      My permz script uses zenity to ask the user to select a choice from a list, for example.

      Comment by IgnorantGuru | September 17, 2011

  3. i am dumber than a bagof hammers when it comes to scripts and their use, so I say “Thank you very much for your web site and article, Mr. Guru.” I have it bookmarked and will stopping by a lot to learn what ever you can teach guys like me.
    Sincerely,
    Rockets Redglare

    Comment by JC Bradshaw | April 12, 2012


Follow

Get every new post delivered to your Inbox.

Join 146 other followers