Understand Linux Shell and Basic Shell Scripting Language Tips-Part I

Picture speak more than words and the below picture says all about the working of Linux.

 

Understanding Linux Shell

Understanding Linux Shell

Read Also

  1. 5 Shell Scripts to Learn Shell Programming – Part II
  2. Sailing Through The World of Linux BASH Scripting – Part III

Understanding Linux Shell

  1. Shell: A Command-Line Interpretor that connects a user to Operating System and allows to execute the commands or by creating text script.
  2. Process: Any task that a user run in the system is called a process. A process is little more complex than just a task.
  3. File: It resides on hard disk (hdd) and contains data owned by a user.
  4. X-windows aka windows: A mode of Linux where screen (monitor) can be split in small “parts” called windows, that allow a user to do several things at the same time and/or switch from one task to another easily and view graphics in a nice way.
  5. Text terminal: A monitor that has only the capability of displaying text stuff, no graphics or a very basic graphics display.
  6. Session: Time between logging on and logging out of the system.

Types of Shell on a Standard Linux Distribution

Bourne shell : The Bourne shell was one of the major shells used in early versions and became a de facto standard. It was written by Stephen Bourne at Bell Labs. Every Unix-like system has at least one shell compatible with the Bourne shell. The Bourne shell program name is “sh” and it is typically located in the file system hierarchy at /bin/sh.

C shell: The C shell was developed by Bill Joy for the Berkeley Software Distribution. Its syntax is modelled after the C programming language. It is used primarily for interactive terminal use, but less frequently for scripting and operating system control. C shell has many interactive commands.

Beginning the Fun! (Linux Shell)

There exist thousands of commands for command-line user, how about remembering all of them? Hmmm! Simply you can not. The real power of computer is to ease the ease your work, you need to automate the process and hence you need scripts.

Scripts are collections of commands, stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard. The shell also provides a variety of useful programming features to make scripts truly powerful.

Basics of Shell Programming

  1. To get a Linux shell, you need to start a terminal.
  2. To see what shell you have, run: echo $SHELL.
  3. In Linux, the dollar sign ($) stands for a shell variable.
  4. The ‘echo‘ command just returns whatever you type in.
  5. The pipeline instruction (|) comes to rescue, when chaining several commands.
  6. Linux commands have their own syntax, Linux won’t forgive you whatsoever is the mistakes. If you get a command wrong, you won’t flunk or damage anything, but it won’t work.
  7. #!/bin/sh – It is called shebang. It is written at the top of a shell script and it passes the instruction to the program /bin/sh.

About shell Script

Shell script is just a simple text file with “.sh” extension, having executable permission.

Process of writing and executing a script
  1. Open terminal.
  2. Navigate to the place where you want to create script using ‘cd‘ command.
  3. Cd (enter) [This will bring the prompt at Your home Directory].
  4. touch hello.sh (Here we named the script as hello, remember the ‘.sh‘ extension is compulsory).
  5. vi hello.sh (nano hello.sh) [You can use your favourite editor, to edit the script].
  6. chmod 744 hello.sh (making the script executable).
  7. sh hello.sh or ./hello.sh (running the script)
Writing your First Script
#!/bin/bash
# My first script

echo "Hello World!"

Save the above lines on a text file, make it executable and run it, as described above.

Sample Output
Hello World!

In the above code.

#!/bin/bash (is the shebang.)
# My first script (is comment, anything following '#' is a comment)
echo “Hello World!” (is the main part of this script)
Writing your Second Script

OK time to move to the next script. This script will tell you, your’s “username” and list the running processes.

#! /bin/bash
echo "Hello $USER"
echo "Hey i am" $USER "and will be telling you about the current processes"
echo "Running processes List"
ps

Create a file with above codes, save it to anything you want, but with extension “.sh“, make it executable and run it, from you terminal.

Sample Output
Hello tecmint
Hey i am tecmint and will be telling you about the current processes
Running processes List
  PID TTY          TIME CMD
 1111 pts/0    00:00:00 bash
 1287 pts/0    00:00:00 sh
 1288 pts/0    00:00:00 ps

Was this cool? Writing script is as simple as getting an idea and writing pipelined commands. There are some restrictions, too. Shell scripts are excellent for concise filesystem operations and scripting the combination of existing functionality in filters and command line tools via pipes.

When your needs are greater – whether in functionalityrobustnessperformance,efficiency etc – then you can move to a more full-featured language.

If you already know C/Perl/Python programming language or any other programming language, learning the scripting language won’t be much difficult.

Writing your Third Script

Moving to, write our third and last script for this article. This script acts as an interactive script. Why don’t you, yourself execute this simple yet interactive script and tell us how you felt.

#! /bin/bash
echo "Hey what's Your First Name?";
read a;
echo "welcome Mr./Mrs. $a, would you like to tell us, Your Last Name";
read b;
echo "Thanks Mr./Mrs. $a $b for telling us your name";
echo "*******************"
echo "Mr./Mrs. $b, it's time to say you good bye"
Sample Output
Hey what's Your First Name?
Avishek
welcome Mr./Mrs. Avishek, would you like to tell us, Your Last Name
Kumar
Thanks Mr./Mrs. Avishek Kumar for telling us your name
******************************************************
Mr./Mrs. Kumar, it's time to say you good bye

Well this is not an end. We tried to bring a taste of scripting to you. In our future article we will elaborate this scripting language topic, rather a never ending scripting language topic, to be more perfect. Your valuable thoughts in comments is highly appreciated, Like and share us and help us to spread. Till then just chill, keep connected, stay tuned.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是最新版Understand-Chinese-4.0.922-Linux 中文版 http://www.scitools.com/products/understand/ Understand 软件的功能主要定位于代码 代码的阅读理解。界面貌似是用 Qt 开发的。 代码 具备如下特性: 1、支持多语言:Ada, C, C++, C#, Java, FORTRAN, Delphi, Jovial, and PL/M ,混合语言的 project 也支持 2、多平台: Windows/Linux/Solaris/HP-UX/IRIX/MAC OS X 3、代码 代码语法高亮、代码 代码折叠、交叉跳转、书签等基本阅读功能。 代码 代码 4、可以对整个 project 的 architecture、metrics 进行分析 分析并输出报表。 分析 5、可以对代码 代码生成多种图(butterfly graph、call graph、called by graph、control flow graph、 代码 UML class graph 等) ,在图上点击节点可以跳转到对应的源代码 代码位置。 代码 6、 提供 Perl API 便于扩展。 作图全部是用 Perl 插件实现的, 直接读取分析 分析好的数据库作图。 分析 7、内置的目录和文件比较器。 8 、支持 project 的 snapshot,并能和自家的 TrackBack 集成便于监视 project 的变化。 小技巧(官网的 FAQ 里有) : 1、设置字体和颜色风格 修改默认字体:Tools -> Options -> Editor -> Default style 修改颜色: Tools -> Options -> Editor -> Styles 2、生成 UML 类图、调用树图 默认安装的插件不支持这两种图,需要从官网下载插件。 _http://www.scitools.com/perl_scripts/uperl/uml_class.upl _http://www.scitools.com/perl_scripts/uperl/invocation.upl 放到 sti/conf/scripts/local 目录下。 然后重新运行,执行 project-> project graphical views -> xxxx 可以生成这两种图。 3、更改图的字体 直接修改对应的脚本文件(\Program Files\STI\conf\scripts 目录下) ,在 do_load( )函数的对应 位置加入如下的设置: $graph->default("fontname","Consolas","node"); $graph->default("fontsize","10","node"); $graph->default("fontname","Consolas","edge"); $graph->default("fontsize","10","edge"); 注意:有的脚本中的作图变量名不是 $graph 而是 $g。 另外一款代码 代码可视化理解工具

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值