Shell in a nutshell - Introduction
Brief Introduction: A Shell Script
A shell script is very light and simple language compared to others such as C, C++, or Java but still powerful to handle numerous tasks efficiently. Strictly speaking, a shell script is a command line interpreter designed to be run by the Unix shell rather than stand alone computer languages as I mentioned above. It means that shell activates the process as soon as the user types the command on the command line (In C or C++, you cannot type the command as shell does).
This is Terminal on Mac OSX.
Let’s look at the example how shell scripts are used (I opened Terminal in Mac OSX, which connects me to the command line system). Printing a simple sentence, or saving it as a text file cannot be easier than this (In C++, printing a simple sentence needs at least about 5 lines).
# Print a sentence. "echo" is a printing function.
echo "My name is Hyungwon Yang."
# Save the sentence as a text file. The sentence is saved as "name.txt" file.
echo "My name is Hyungwon Yang." > name.txt
Furthermore, the user is able to connect many scripts written in various computer languages into one shell script in order to tackle down bigger tasks. Instead of running a bunch of scrips by yourself and then waiting until the job is finished, preparing a shell script that controls all those tasks might be convenient.
Look closely at the script below. Shell prints out the processing information and delivers text files to each function until it is saved as “final.txt”
# First data processing by python. "first_process.py"(python) gets input as "dataset.txt" and returns output as "1st_proc.txt"
echo "First data processing..."
python first_process.py dataset.txt 1st_proc.txt
# Second data procesing by C++. "second_process"(c++) gets input as "1st_proc.txt" and returns output as "2nd_proc.txt"
echo "Second data processing..."
./second_process 1st_proc.txt 2nd_proc.txt
# Final sorting the dataset. "cat" function prints all the written strings in the "2nd_proc.txt" file and it is being sent by "|" pipe symbol and then sorts the strings. Lastly it saves the sorted strings as "final.txt"
echo "Sorting the dataset."
cat 2nd_proc.txt | sort > final.txt
Many programmers including me work with Linux-like environment (like Ubuntu, Mac terminal) and use a shell script so as to run the programs, manipulate files, and develop softwares. It might be difficult to learn a shell script at the first time, but once you get familiar with it, shell will alleviate your painful and laborious scripting jobs and optimize your work.
댓글
댓글 쓰기