Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Shell Scripting early experience     - To install the Ubuntu Touch emulator on Ubuntu (Linux)

- Install snort intrusion detection system on Debian (Linux)

- Linux Tutorial ---- fundamentals of data traffic redirection (Linux)

- Debian GNU / Linux service list acquisition, shutting down services or run (Linux)

- Linux operating system security management skills notes (Linux)

- How to use static, class, abstract method in Python (Programming)

- Linux / Windows setup is complete port range (Linux)

- Linux server is how to do after the invasion (Linux)

- Oracle PL / SQL selective basis (IF CASE), (LOOP WHILE FOR) (Database)

- RHEL5 establish a local yum source (Linux)

- Ftp user to create multiple virtual machines to support different access rights Examples (Server)

- Oracle table space usage monitoring (Database)

- Linux mount command Detailed (Linux)

- Linux system security knowledge (Linux)

- How to Install Foreman under Ubuntu (Server)

- Slice MyCAT common fragmentation rules of enumeration (Database)

- MySQL event table to achieve timing build a small note (Database)

- IDS Intrusion Detection System built (Linux) (Linux)

- Linux environment variable configuration (Linux)

- Inject script commands manually annotated summary (Linux)

 
         
  Shell Scripting early experience
     
  Add Date : 2018-11-21      
         
       
         
  Generally, when people refer to "shell scripting language" when they come to mind is bash, ksh, sh, or other similar linux / unix scripting language. Scripting language is another way to communicate with the computer. Use graphical window interface (either windows or linux does not matter) the user can move the mouse and click on various objects, such as buttons, list boxes, and so on. But in this way every time the user wants to computer / server to complete the same task (for example, batch convert photos, or download new movies, mp3, etc.) but it is very inconvenient. To get all these things simple and automated, we can use shell script.

Some programming languages, like pascal, foxpro, C, java and the like, before execution need to be compiled. They need the right compiler to make our code to complete a task.

Some other programming languages such as php, javascript, visualbasic like, you do not need the compiler, so they need an interpreter, and we do not need to compile the code to run the program.

shell script interpreter like the same, but it is usually used to call external compiled programs. Then it will capture the output, the exit code and processed according to the situation.

Linux world, one of the most popular shell scripting language is bash. And I think (this is my own opinion) The reason is that, by default bash shell lets users easily through the history command (executed earlier) navigation In contrast, ksh is required to make some adjustments .profile or remember some key "magic" combination of access to historical and correct command.

Well, I think these presentations have been enough, and the rest to the environment which is best for you, just leave it to you to judge. From now on, I will talk about and bash scripts. In the following example, I will use CentOS 6.6 and bash-4.1.2. Make sure you have the same version, or a later version.

Shell script stream

shell scripting language and just a few people to talk to similar. You just think of all the commands of those who help you do things, as long as you use the right way to ask them to do it. For example, you want to write the document. First of all, you need paper. Then you need to say to someone content to listen, let him help you write. Finally, you want to store it somewhere. Or say you want to build a house, so you need to ask the right people to clear the site. They say "something done" and then other engineers can help you come to a wall. Finally, when these engineers also tell you "thing finished the job", you can call the painter to give the house a whitewash. If you let the front wall painter Qi Hao came whitewash, what will happen? I think they will start complaining. Almost all of these commands are like people talk, if they completed the job without any problems, then they will tell "standard output." If they can not do what you told them to do - they will tell "standard error." Thus, finally, all commands via the "standard input" to listen to you.

Quick example - when you open the linux terminal and write some text - you are the "standard input" and bash speak. Well, let's ask the bash shell who am i (Who am I?) It.

root @ localhost ~] # who am i < --- you via standard input to bash shell
root pts / 02015-04-2220: 17 (192.168.1.123) < --- bash shell by to answer your standard output
Now, let's say a few bash did not understand the question:

[Root @ localhost ~] # blablabla < --- Ha, you're in and speak to the standard input
-bash: blablabla: command not found < --- bash by standard error complaining
":" Before the first word is usually complain to your commands. In fact, each of these streams have their own index number (LCTT Annotation: file handle number):

Standard input (stdin) - 0
Standard output (stdout) - 1
Standard error (stderr) - 2
If you really want to know which output command said something - you need to be redirected to that statement (after the command greater than ">" and flow index) file:

[Root @ localhost ~] # blablabla 1> output.txt
-bash: blablabla: command not found
In this case, we try to redirect the stream 1 (stdout) to a file called output.txt. The contents of the file to get things done that we look at it, you can do it using the cat command:

[Root @ localhost ~] # cat output.txt
[Root @ localhost ~] #
It seems to be empty. Okay, now let's redirect the flow 2 (stderr):

[Root @ localhost ~] # blablabla 2> error.txt
[Root @ localhost ~] #
Well, we did not see complaints words. Let's examine that file:

[Root @ localhost ~] # cat error.txt
-bash: blablabla: command not found
[Root @ localhost ~] #
Sure enough! We see that all complaints are then recorded to a file errors.txt went inside.

Sometimes, the command will also generate stdout and stderr. To redirect them to a different file, we can use the following statement:

command 1> out.txt 2> err.txt
To shorten that statement, we can ignore the "1" because by default stdout will be redirected:

command> out.txt 2> err.txt
Okay, let's try to do "bad things." Let us use the rm command file1 and folder1 to delete it:

[Root @ localhost ~] # rm -vf folder1 file1> out.txt 2> err.txt
Now check the following output files:

[Root @ localhost ~] # cat out.txt
removed `file1 '
[Root @ localhost ~] # cat err.txt
rm: can not remove `folder1 ': Is a directory
[Root @ localhost ~] #
As we have seen, different streams are separated into different. Sometimes this is not very convenient, because we want to see if an error occurs, what has occurred in succession before or after certain operations. To achieve this goal, we can redirect the flow to the same two files:

command >> out_err.txt 2 >> out_err.txt

Note: Please note that I use ">>" replaced ">." It allows us to append to the file instead of overwriting the file.

We can also redirect a flow to another:

command> out_err.txt 2> & 1
Let me explain. Standard output from all commands are redirected to out_err.txt, error output will be redirected to the stream 1 (already explained above), and the flow will be redirected to the same file. Let's look at this example:

[Root @ localhost ~] # rm -fv folder2 file2> out_err.txt 2> & 1
[Root @ localhost ~] # cat out_err.txt
rm: can not remove `folder2 ': Is a directory
removed `file2 '
[Root @ localhost ~] #
Looking at the output of these combinations, we can be described as follows: First, try the rm command will delete folder2, but it will not succeed, because linux -r key requirement to allow the rm command to delete a folder, while the second will file2 been deleted. By providing rm -v (details) keys, we let the rm command to tell us each file or folder is deleted.

Which is what you need to know about redirect almost all the content. I say almost, because there is a more important redirection tool called the "pipeline." By using the | (pipe) symbol, we usually redirect stdout stream.

For example, we have a text file:

[Root @ localhost ~] # cat text_file.txt
This line does not contain H e l l o word
This lilne contains Hello
This also containd Hello
This one no due to HELLO all capital
Hello bash world!
And we need to find some of the line with the "Hello", and in Linux there is a grep command to complete the work:

[Root @ localhost ~] # grep Hello text_file.txt
This lilne contains Hello
This also containd Hello
Hello bash world!
[Root @ localhost ~] #
When we have a file that you want to search the inside of the time, this is very good with them. As if we need to find something in the output of another command, how should we do this? Yes, of course, we can redirect the output to a file, and then locate the file inside:

[Root @ localhost ~] # fdisk -l> fdisk.out
[Root @ localhost ~] # grep "Disk / dev" fdisk.out
Disk / dev / sda: 8589 MB, 8589934592 bytes
Disk / dev / mapper / VolGroup-lv_root: 7205 MB, 7205814272 bytes
Disk / dev / mapper / VolGroup-lv_swap: 855 MB, 855638016 bytes
[Root @ localhost ~] #
If you intend to grep number of double quotation marks with the contents of the space it!

Note: fdisk command displays information about the Linux operating system disk drive.

As we have seen, this way is very convenient, because we soon put a temporary file space to the mess. To accomplish this task, we can use the pipeline. They allow us to redirect a command's stdout stream to stdin to another command:

[Root @ localhost ~] # fdisk -l | grep "Disk / dev"
Disk / dev / sda: 8589 MB, 8589934592 bytes
Disk / dev / mapper / VolGroup-lv_root: 7205 MB, 7205814272 bytes
Disk / dev / mapper / VolGroup-lv_swap: 855 MB, 855638016 bytes
[Root @ localhost ~] #
As you can see, we do not need any temporary files to get the same results. We fdisk stdout redirected to grep stdin.

Note: Redirection is always piping from left to right.

There are several other redirection, but we'll put them in the back talk.

Display a custom message in the shell

As we know, in general, the exchange and the exchange of shell and the inner shell of the dialogue is conducted in a way. So, let's create some real scripts it, these scripts will talk to us. This will make you learn some simple commands, scripts and concepts have a better understanding.

Suppose that we are the front desk manager of a company, we want to create a shell script to register call information: a brief description of the phone number, user name, and the problem. We are going to store this information to a plain text file data.txt in for future statistics. Script itself is working through dialogue, which makes the easy life of staff at the front desk a nice relaxing spot. Well, first we need to display questions. For show information, we can use the echo and printf commands. Both are used to display information, but printf is more powerful, because we can use it nicely formatted output, we can make it right-aligned, left-aligned or set aside specifically for the information space. Let's begin with a simple example. To create the file, use your favorite text editor (kate, nano, vi, ......), and then create a file called note.sh, which writes these commands:

echo "Phone number?"

How to run / execute a script?

After you save the file, we can use the bash command to run our file as its argument:

[Root @ localhost ~] # bash note.sh
Phone number?
In fact, this script is executed inconvenient. If you do not use bash command as a prefix to execute, it will be more comfortable. To make the script executable, we can use the chmod command:

[Root @ localhost ~] # ls -la note.sh
. -rw-R - r - 1 root root 22Apr2320: 52 note.sh
[Root @ localhost ~] # chmod + x note.sh
[Root @ localhost ~] # ls -la note.sh
-rwxr-xr-x.1 root root 22Apr2320: 52 note.sh
[Root @ localhost ~] #
Note: ls command displays the files in the current folder. By adding -la key, it will display more file information.

As we have seen, before the chmod command is executed, the script only read (r) and write (w) permission. After the execution of chmod + x, it won the execute (x) permission. (For more details on permission, I will tell in the next article.) Now we just need to run so:

[Root @ localhost ~] #. / Note.sh
Phone number?
Before the name of the script, I added ./ combination. . (Dot) means that the current position (current folder) in the unix world, / (slash) is a folder separator. (In Windows systems, we use the backslash \ indicates the same function) So, this whole combination mean: "The Executive note.sh script from the current folder." I think if I use the full path to run this script, you will be more clearer:

[Root @ localhost ~] # / root / note.sh
Phone number?
[Root @ localhost ~] #
It can also work.

If all linux users have the same default shell, then everything is OK. If we just execute the script, the default user shell will be used to parse the contents of the script and run the command. Different shell syntax, internal order and so has a little bit different, so, in order to ensure that our script using bash, we should add #! The first line / bin / bash to a file. Thus, the default user shell invokes / bin / bash, and only at that time, the script commands will be executed:

[Root @ localhost ~] # cat note.sh
#! / Bin / bash
echo "Phone number?"
Until now, we will be 100% confident bash script used to parse our content. let's continue.

Read input

After the display information, the script will wait for the user to answer. There is a read command to receive the user's answer:

#! / Bin / bash
echo "Phone number?"
read phone
After the execution, the script will wait for user input until the user presses [ENTER] key to finish input:

[Root @ localhost ~] #. / Note.sh
Phone number?
12345 < --- here is the content of my input
[Root @ localhost ~] #
Everything you enter will be stored in the variable phone, the value of the variable to be displayed, we can also use the echo command:

[Root @ localhost ~] # cat note.sh
#! / Bin / bash
echo "Phone number?"
read phone
echo "You have entered $ phone as a phone number"
[Root @ localhost ~] #. / Note.sh
Phone number?
123456
You have entered 123456as a phone number
[Root @ localhost ~] #
In the bash shell, in general, we use the $ (dollar) sign to indicate that this is a variable, in addition to read into the other one of the few variables and when they do not need this $ (will be explained in the future).

Well, now we are ready to add the rest of the problem:

#! / Bin / bash
echo "Phone number?"
read phone
echo "Name?"
read name
echo "Issue?"
read issue
[Root @ localhost ~] #. / Note.sh
Phone number?
123
Name?
Jim
Issue?
script isnot working.
[Root @ localhost ~] #
 
Use Stream Redirector

so perfect! Everything is left to redirected to the file data.txt. As the field separator, we will use / (slash) symbol.

Note: You can choose whatever you think is best delimiter, but to ensure that the contents of the file will not contain these symbols included, otherwise it will lead to additional fields in the text line.

Do not forget to use the ">>" instead of ">", because we want to append to the end of the output file!

[Root @ localhost ~] # tail -2 note.sh
read issue
echo "$ phone / $ name / $ issue" >> data.txt
[Root @ localhost ~] #. / Note.sh
Phone number?
987
Name?
Jimmy
Issue?
Keybord issue.
[Root @ localhost ~] # cat data.txt
987 / Jimmy / Keybord issue.
[Root @ localhost ~] #
Note: tail command displays the last n lines of a file.

Get. Let's take a look at run time:

[Root @ localhost ~] #. / Note.sh
Phone number?
556
Name?
Janine
Issue?
Mouse was broken.
[Root @ localhost ~] # cat data.txt
987 / Jimmy / Keybord issue.
556 / Janine / Mouse was broken.
[Root @ localhost ~] #
Our file is growing, let us add a line in front of every date it, playing with it for the future when these statistics would be useful. To achieve this function, we can use the date command and specify a format, because I do not like the default format:

[Root @ localhost ~] # date
ThuApr2321: 33: 14 EEST 2015 default output < ---- date command
[Root @ localhost ~] # date "+% Y.% m% d% H:.% M:% S"
2015.04.2321: 33: 18 < ---- formatted output
There are several ways to read the output of the command to a variable, in this simple case, we will use `(the backtick, not a single quotation marks, and tilde ~ key in the same position):

[Root @ localhost ~] # cat note.sh
#! / Bin / bash
now = `date" +% Y.% m% d% H:.% M:% S "`
echo "Phone number?"
read phone
echo "Name?"
read name
echo "Issue?"
read issue
echo "$ now / $ phone / $ name / $ issue" >> data.txt
[Root @ localhost ~] #. / Note.sh
Phone number?
123
Name?
Jim
Issue?
Script hanging.
[Root @ localhost ~] # cat data.txt
2015.04.2321: 38: 56/123 / Jim / Script hanging.
[Root @ localhost ~] #
Ah ...... Our script looks a bit ugly ah, let us beautify it. If you want to read the read command manually, you will find a read command can also display some information. To achieve this, we should use the -p add key information:

[Root @ localhost ~] # cat note.sh
#! / Bin / bash
now = `date" +% Y.% m% d% H:.% M:% S "`
read -p "Phone number:" phone
read -p "Name:" name
read -p "Issue:" issue
echo "$ now / $ phone / $ name / $ issue" >> data.txt
You can search directly from the console to a lot of interesting information about each command, simply type: man read, man echo, man date, man ......

Agree? It appears to be more comfortable!

[Root @ localhost ~] #. / Note.sh
Phone number: 321
Name: Susane
Issue: Mouse was stolen
[Root @ localhost ~] # cat data.txt
2015.04.2321: 38: 56/123 / Jim / Script hanging.
2015.04.2321: 43: 50/321 / Susane / Mouse was stolen
[Root @ localhost ~] #
Cursor behind the message (not in the new line), which is a bit mean. (LCTT Annotation: If you use the echo command output display, you can use the -n option to avoid line breaks.)

cycle

It is time to improve our script. If you answer the phone all day, if every time to run, it would not be too much trouble? Let us make these activities go on endlessly cycle:

[Root @ localhost ~] # cat note.sh
#! / Bin / bash
whiletrue
do
read -p "Phone number:" phone
now = `date" +% Y.% m% d% H:.% M:% S "`
read -p "Name:" name
read -p "Issue:" issue
echo "$ now / $ phone / $ name / $ issue" >> data.txt
done
I have read phone switched positions and now = date line. This is because I want to get the phone number and then enter the time. If I put it in the first line of the loop, the loop after the first, now will be in the variable data stored in the file immediately after the acquisition time. And this is not good, because the next time may be 20 minutes after the call, or even later.

[Root @ localhost ~] #. / Note.sh
Phone number: 123
Name: Jim
Issue: Script still not works.
Phone number: 777
Name: Daniel
Issue: I broke my monitor
Phone number: ^ C
[Root @ localhost ~] # cat data.txt
2015.04.2321: 38: 56/123 / Jim / Script hanging.
2015.04.2321: 43: 50/321 / Susane / Mouse was stolen
2015.04.2321: 47: 55/123 / Jim / Script still not works.
2015.04.2321: 48: 16/777 / Daniel / I broke my monitor
[Root @ localhost ~] #
Note: Exit from the infinite loop, you can press [Ctrl] + [C] key. Shell will display ^ represents the CTRL key.

Use a pipe to redirect

Let's add more features to our "Frankenstein (Frankenstein)", I want to display a script statistics after each call. For example, I want to see each number called me several times. For this, we should cat file data.txt:

[Root @ localhost ~] # cat data.txt
2015.04.2321: 38: 56/123 / Jim / Script hanging.
2015.04.2321: 43: 50/321 / Susane / Mouse was stolen
2015.04.2321: 47: 55/123 / Jim / Script still not works.
2015.04.2321: 48: 16/777 / Daniel / I broke my monitor
2015.04.2322: 02: 14/123 / Jimmy / New script also not working !!!
[Root @ localhost ~] #
Now, all we can redirect the output commands to cut, so cut to the cut line of each piece (we use the separator "/"), and then print the second field:

[Root @ localhost ~] # cat data.txt | cut -d "/" - f2
123
321
123
777
123
[Root @ localhost ~] #
Now we can redirect the output of this command to fight another sort:

[Root @ localhost ~] # cat data.txt | cut -d "/" - f2 | sort
123
123
123
321
777
[Root @ localhost ~] #
Then leaving only unique rows. To count unique entries, simply add -c key to uniq command:

[Root @ localhost ~] # cat data.txt | cut -d "/" - f2 | sort | uniq -c
3123
1321
1777
[Root @ localhost ~] #
Just add this to the end of our loop:

#! / Bin / bash
whiletrue
do
read -p "Phone number:" phone
now = `date" +% Y.% m% d% H:.% M:% S "`
read -p "Name:" name
read -p "Issue:" issue
echo "$ now / $ phone / $ name / $ issue" >> data.txt
echo "===== We got calls from ====="
cat data.txt | cut -d "/" - f2 | sort | uniq -c
echo "--------------------------------"
done
run:

[Root @ localhost ~] #. / Note.sh
Phone number: 454
Name: Malini
Issue: Windows license expired.
===== We got calls from =====
3123
1321
1454
1777
--------------------------------
Phone number: ^ C
The current scene throughout the several well known steps:

The message
Get user input
Stored value to the file
Data processing storage
However, if you a little sense of responsibility, he sometimes needs to input data, and sometimes need to count, or might be looking for something in the data stored in it? For these things, we need to use switches / cases, and know how to nicely formatted output. This time the shell "draw" the table is useful.
     
         
       
         
  More:      
 
- Linux serial debugging tools xgcom install (Linux)
- to compile FFmpeg In Ubuntu (Linux)
- Create RAID 1 (mirroring) with two disks (Linux)
- HTML5 postMessage cross-domain data exchange (Programming)
- Mac OS X 10.9 compiler OCI8 module (Programming)
- To compile and install Redis Linux and master-slave replication configuration (Database)
- Use netcat [nc] command on Linux and Unix port scan (Server)
- Bash How to read a file line by line (Programming)
- Several Ceph performance optimization of new methods and ideas (2015 SH Ceph Day after flu reference) (Server)
- Android engineers interview questions (Programming)
- Linux Man Page Installation (Linux)
- The Linux firewall is configured to use proxy (Linux)
- Spring JDBC Comments (Programming)
- MySQL server after an unexpected power outage can not start (Database)
- How to use the ps command to monitor progress in the implementation of Linux commands (Linux)
- Struts2 dynamic call DMI and error Solution (Programming)
- Denyhosts prevent hackers using SSH scanning (Linux)
- The OpenGL ES GLFW window structures (Programming)
- Debian 8 (amd64) installation deployment Memcached management tools MemAdmin (Server)
- ORA-12154 TNS could not resolve the specified identifier (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.