leo charre


intro to scripting in linux tutorial

In posix (linux), the command line is king.
The command line is a.k.a. cli, acronym for Command Line Interface.

I’m all about speeding things up. Letting the computer do, what I don’t want to have to do.
Little repetitive things do add up.

You can add commands to your system by creating scripts.
A script in this case is just a text file that has code (instructions), and gets treated as a program- just as find and ls and cd are available to you, you can create as many new commands as you want.
Everything you type into the cli (shell, terminal, command line, gnome terminal), can be written into a text file and executed as a program.

You can use various languages to code a script that will do something.
One of the most powerful aspects of living with posix is that you can add these scripts and customize the system to your liking. (In fact, various people can on the same system without conflict! But that is another matter… )

Some Terminology..

cli
Command Line Interface (cee el eye). This term is used in various ways. A program you interact with via a web browser is not a cli, (it's a wui). The browser itself (firefox, mozilla, etc) is not cli either, it is gui. A program such as ls, which you open a terminal and type stuff out to, is a cli. The terminal is the command line- a script can be cli, the terminal itself is cli. The prompt is the 'caret' (not cursor).
wui
Web User Interface (wooey). Uses a browser to take input and display output- Facebook, amazon, ebay, google, these are all wuis (wooies).
gui
Graphical User Interface (gooey). This uses the "mouse" (cursor). kde, gnome, all graphical desktop suites- are gui. Video games are gui. Adobe Photoshop is gui. Quicken is gui. Your web browser is not wui, it is gui. Why? Because you are not using a web browser via a web browser- that would be a gui wui (gooey wooey).
posix is linux is unix
posix is Portable Operating System for Unix. The term was coined by Richard Stallman. posix is unix is linux is bsd is darwin. If any of the autorities on one of these read that line, they would vomit- but as far as you are concerned at this moment- linux is posix is unix is linux is posix.

HOW TO MAKE A SCRIPT

First you should realize that anything you can do via the command line (aka terminal, aka shell) you can save in a script and it will be performed.
Let’s take a primal example. Imagine you want to list what files are in your Desktop directory at any time from anywhere. Let’s use the ‘-1′ option flag for single line..

The command is

ls -1 ~/Desktop

Great. Now let’s turn that into a command. We’ll open up a new file with vim..

# vim ~/bin/desk

The squiggly line gets translated by the system to ‘/home/yourusername’. You could have also run the command # vim /home/leo/bin/desk

Enter insert mode by hitting the [insert] or [i] key.
Type in..

#!/bin/sh
ls -1 ~/Desktop

Hit escape to exit edit mode.. Type in the [:] key and enter wq and hit enter. That means ‘w’rite ‘q’uit.

The first line means that our program is to be interpreted as what you would do in the command line (the terminal). If we had wanted to code perl or python etc.. we would have used something like #!/usr/bin/perl.

Now, we need to tell the system that this file is not just a file but an executable program. We have to ‘chmod’ it to be executable..

# chmod 0755 ~/bin/desk

Done.
Now if you call..
# desk
You get the same thing as if you called
# ls -1 ~/Desktop

ANOTHER EXAMPLE

I am willing to go the distance.
I develop perl distros. One of the things I do to install from a project directory is..

perl Makefile.PL
make install
make realclean

Going the distance, I created
# vim ~/bin/pinstall

#!/bin/sh
perl Makefile.PL
make install
make realclea

Saved it, then..
# chmod 0755 ~/bin/pinstall
To make it executable.
Now whenever I am in a project, I can call;
# pinstall
Seems insignificant, but I feel it helps me, makes the experience more enjoyable.

SOMETHING MORE USEFUL

Ok, I use kde. Gnome is super, and I should use it, but .. it’s missing some stuff. Like… getting rid of the window borders- setting automatic per X11 window dimensions and behaviour.. etc.

But I also use gnome applications! Like pan, gqview.. Not to mention firefox!
And I would really like to have my font settings follow along.
If you know what I mean- then you know what I mean.. yeah.. it’s a peeve.

So, usually you have to start up (via the gui… with the mouse.. yuk!) font settings, and then wait for it to load up and then close it. Screw that! This is LINUX- WE DONT DO THAT SH1+ HERE! Guis are for users. And you left windows because you’re not a user- you’re a badass mofo and … well… anywho…ahem.

So..
You can create a script to do this for you.
Fire up your trusty vim and create your program.. You can name it whatever you want..

# vim ~/bin/kgnome

Now.. There are two versions under Fedora Core. One is for FC4, another for FC8..

#!/bin/sh
# this is for fc4
# this is a comment because it begins with a #, yes.. I know the first
# line does not work that way.. 

gnome-font-properties & sleep 5 && killall -9 gnome-font-properties &

Ok.. what the hell is that & sign???
That sorta means to release the current environment and continue.
The double && is so uh.. maybe topic for another post.

So, what is happening? We start the process, we wait for 5 seconds, and we kill the process.

What’s way cool is that you can call # kgnome and it will return your terminal prompt immediately and you can continue doing whatever you want- while it works in the background.

And remember you can type in kg[tab] and you won’t have to type in the whole name of the program. Tab completion rules. I heard some windoze version has that now, how long has it been in unixy land? Forever???

Now, in FC8 …

#!/bin/sh
# this is for FC8
gnome-appearance-properties & sleep 5 && killall -9 gnome-appearance-properties &

Automatically run script when KDE starts

Ok, that’s nice. But every time you log into kde and want to apply your gnome font and appearance settings, you would have to call the ‘kgnome’ command.
What you want instead is to place that script, or a script that calls that script, in the ‘~/.kde/Autostart/’ directory.
Make sure to chmod it 0755 to make it executable.
Try it, log out of kde, log back in, and start a gnome based app, like pan or network preferences.

You can do endless things here. What if you wanted to open gaim and 3 gnome terminals when you start kde?
Write: ~/.kde/Autostart/

DETAILS

Extensions

Unix does not care about file extensions. In windoze, you make a script executable by adding a .exe extension. I remember renaming diaryentry.txt to diaryentry.exe and watching windoze try to run it.
None of that in unix.

The contents and permissions of a file determine what will happen with it.

You can have a filename.jpg file that is a program, try it, write;

# vim ~/bin/filename.jpg :

#!/bin/sh
echo "Yes, I am a picture.\n";

# chmod 0755 ~/bin/filename.jpg
# filename

What can I write a script in?

The first line calls a preprocessor to run the program. You could even write a program in php!!!!

# vim ~/bin/whataboutme

#!/usr/bin/php

# chmod 0755 ~/bin/whataboutme
# whataboutme

I made my script executable but it won’t run

If you write a script in /home/silly/myscript.sh for example, and you make a command call..

# myscript.sh

It will not run. Even if you did make it executable.
But the following *will* work..

# /home/silly/myscript.sh

Also this:

# ~/myscript.sh

And if your current working directory is ~
# ./myscript.sh

Why?

When you don’t specify a path to a script, the system looks inside places like /usr/bin and /home/silly/bin for what you are trying to call.
That is how you can write scripts that you can call via the shell just like you would ‘find’ and ‘ls’, without having root access to the machine.
Any executable scripts in /home/yourself/bin directory, can be run without specifying the path.

WHERE TO LEARN MORE

This is a very brief how-to introduction on scripting in unix.
A very excellent in depth book on bash is written and maintained by Mendel Cooper. There are also pdf and tarballs of the content available for download.

One Response to “intro to scripting in linux tutorial”

  1. mad professor leo » Blog Archive » fix gnome fonts in kde Says:

    [...] See also [...]

Leave a Reply


Linux User