Linux Shell Programming – yes


It has been a fun with the script programming, e.g. batch or shell. I have just formatted my desktop and install ubuntu 12. Its graphic interface and lots of utilities make my life easier. If not on the fact that I have to keep windows for my work purpose, I would delete the windows from my laptop as well 🙂

I start to pick up some knowledge of shell programming. The shell programing is very powerful, and in my opinion, it is a real programming language while the batch script in windows can barely be counted one.

The most difference between the batch and shell programming is that the shell supports inherently the while/for loop while in batch, this can be acheived by using the for command (external file). The shell well supports the variable assignment, arithmetic operations and multithreading while in batch, it is difficult to achieve the similar results (not impossible)

Here are some quick notes on linux shell. We choose BASH since it is the most popular (maybe the default shell for ubuntu).

s3 Linux Shell Programming - yes batch script beginner implementation linux Shell Scripts technical tools / utilities

echo $SHELL prints the SHELL variable, which tells you what shell you are currently using.

echo $? prints the last return status (exit code from last program), 0 usually means normal while 130 means interrupted by the keyboard (Ctrl + C)

s1 Linux Shell Programming - yes batch script beginner implementation linux Shell Scripts technical tools / utilities

which is a utility that tells you from what directory that the file will be executed (it searchs the $PATH)

if you type which yes, you will locate a file yes located at /usr/bin/yes. This utility keeps printing ‘y’ forever until killed or interrupted.

The only purpose that I can think of for this small program is that it can be used in junction with other (e.g. through pipe).

For example, yes | rm -i * will remove everything on the current directory (dangerous, don’t try).

s2 Linux Shell Programming - yes batch script beginner implementation linux Shell Scripts technical tools / utilities

To me, yes seems a greeting that tells you that he is ready.

We can easily create a script version of yes utility.

First, type in vi yes to launch the default text-based editor, press i to type in and press :wq to save and exit.

#!/bin/bash
#script version of /usr/bin/yes

while true; do
	echo y
done

Under the command shell, type in chmod +x yes to make it executable.

Happy scripting!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
550 words
Last Post: Notes on Python again
Next Post: File Opening Mode for TFileStream in Delphi

The Permanent URL is: Linux Shell Programming – yes

Leave a Reply