Linux. Shell Programming Notes-basics

Source: Internet
Author: User
Chapter 2 why do I need to pass parameters to shell scripts when passing parameters in shell programming basic scripts? Parameter passing can pass External & amp; 20540; to the internal functions of the script to improve the flexibility of the script. parameter passing can add applicable options of the script to increase the customization of the script, in order to cope with different Chapter 2 Basic shell programming script passing parameters

Why should we pass parameters to shell scripts? Parameter passing can pass external values to the internal functions of the script to improve the flexibility of the script. parameter passing can add applicable options for the script to increase the customization of the script, to cope with different situations.

$ # Indicates the number of parameters

$ @ Indicates the parameter content

If you have more than 9 parameters, you cannot use $ l0 to reference the l0 parameter. First, you must process or save the first parameter ($1), then use the shift command to delete the parameter $1, and move all the remaining parameters one by one, therefore, $10 is changed to $9, and so on. $ # Is updated to reflect the remaining number of parameters. In practice, the most common condition is to iterate the arguments to a function or shell script, or iterate to a command to replace the list created using the for statement. Therefore, this constraint is basically not a problem.

Instance script

[hcr@slave2 temp]$ cat ~/bin/ps.sh#!/bin/sh ps -ef |grep $@
[hcr@slave2temp]$ ps.sh firefoxhcr       2417 1784  0 17:00 pts/0    00:00:00 /bin/sh /home/hcr/bin/ps.shfirefoxhcr       2419 2417  0 17:00 pts/0    00:00:00 grep firefox[hcr@slave2 temp]$
IO redirection standard input, standard output, and standard error

The program reads the input (data source), outputs after calculation (data destination), and reports exceptions and errors, which are standard input) standard output and standard error ).

Many unix programs follow this principle-reading from standard input, processing, and output from standard output. A program that follows this principle is often called a filter ). Filters and pipelines are in the unix world: and the wide-forward and strict procedures are praised.

Pipelines and redirection

Read from standard input, output from standard output, and report exceptions and errors to standard errors. This is the correct behavior of software that follows the UNIX philosophy. However, we cannot concentrate all input and output in the dark character Field. we also need to read and write files, watch videos, listen to music, and play games, the input and output of human-computer interaction come from different devices or files.

Therefore, shell provides several syntaxes and tags to change the default input and output.

1.> standard output (delete B .txt)

[hcr@slave2 temp]$ echo 'abc' > b.txt

2.<Change standard input

[hcr@slave2 temp]$ cat < b.txt > c.txt 

3.>Append the file content (the file content will not be deleted)

[hcr@slave2 temp]$ cat < b.txt >> c.txt 

4. | create an MPs queue

[hcr@slave2 temp]$ head -n10 /etc/passwd | grep bash 

5. small instances

[hcr@slave2 temp]$ cat /etc/passwd | while read line ; doecho $line >> c.txt; done;

Data sharing of pipelines is achieved in the Linux kernel through memory copy. Compared with CPU operations, moving data to shards consumes more time. Therefore, when designing pipelines, we should try to reduce the amount of data as much as possible than the pipeline's front-end operations. In this way, data copying is fast. Second, program computing workload is reduced. for example, before sort, using grep to find relevant data can reduce the computing workload of many sort tasks.

File descriptor

The kernel uses filedescriptor to access files. The file descriptor is a non-negative integer. Open

When an existing file or a new file is created, the kernel returns a file descriptor. To read and write a file, you also need to use the file descriptor to specify the file to be read and written.

Wonderful use of special files

1./dev/null

/Dev/nul is a black hole. When some files do not want to output standard output or error output, you can specify the output to the black hole.

Instance:

cat /etc/passwd | while read line ; do cat$line 2>/dev/null; done;

If 2>/dev/null is not added, some messages will be reported.

Cat: root: x: 0: 0: root:/bin/bash: No such file or directory

.....

If you don't want to see such information, you can put the error information into the black hole.

1> redirect standard output

2> error standard output

&> Indicates standard output and error standard output.

2./dev/zero

The main purpose of/dev/zero is to create a file with a specified length and is initialized as null. such files are generally used as temporary swap files.

3./dev/tty

/Dev/tty is a very practical file. When the program opens this file, the terminal before UNIX/LINUX. The output information is only displayed on the current active monitor. In some cases (for example, if a script is set to output to/dev/null), you can call this device to display important information on the current terminal, write important information. In this way, the information can be forcibly displayed to the terminal.

#! /Bin/sh

Printf "Enter new PWD"
Stty-echo
Read pass </dev/tty
Printf "Enter new PWD agin"
Read pass2 </dev/tty
Stty echo
If ["$ pass"-eq "$ pass2"]; then
Echo "right"
Else
Echo "wrong"
Exit 1
Fi
Exit 0

Basic text search

Grep command to retrieve text

For details, refer:

Http://www.cnblogs.com/end/archive/2012/02/21/2360965.html

Design and programming of unix and linux systems

Everything is a file

File type

Http://www.cnblogs.com/acpp/archive/2009/12/05/1617547.html

Basic unix Programming Principles

KISS principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.