Discover unix shell scripting examples, include the articles, news, trends, analysis and practical advice about unix shell scripting examples on alibabacloud.com
file from line 22nd to line 33rd to the end of line 44th.
Sed ' 1,21h;22h;23,33h;44g ' pass
18, move the file from line 22nd to line 33rd to the end of line 44th.
Sed ' 22{h;d};23,33{h;d};44g ' pass
19, only the first word of each line is displayed.
Sed-nr ' s/([^a-z]*) ([a-z]+) ([^a-z]+] (. *)/\2/p '/etc/passwd
20, print the first word and the third word of each line.
Sed-nr ' s/([^a-z]*) ([a-z]+] ([^a-z]+) ([a-z]+] ([^a-z]+) ([a-z]+] (. *)/\2
What is a shell script sampleLet's look at an example:#!/bin/shcd ~mkdir Shell_tutcd shell_tutfor (i=0; iExample explanation
Line 1th: Specify the script interpreter, here is the interpreter with/bin/sh
Line 2nd: Switch to the current user's home directory
Line 3rd: Create a directory Shell_tut
Line 4th: Switch to the Shell_tut directory
Line 5th: Cycle conditions, 10 cycles
Line 6th: Create a test_1 ... 10.txt file
Li
the default format for shell scripts written in Windows systems is DOS instead of UNIX, resulting in incorrect output results.Look at the format of the shell script and edit the shell script with VI. Then enter ": Set FF?" If the output end is "Fileformat=dos", you need to modify the format of the file// modify file f
)Redirect in-depth explanationIn general, each Unix/linux command will open three files when it is run:
Standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default.
Standard output file (stdout): StdOut file descriptor for 1,unix program defaults to stdout output data
Standard error file (stderr): The st
Http://www.runoob.com/linux/linux-shell.htmlShell Pass ParametersIn addition, there are several special characters for handling parameters:
parameter Handling
Description
$#
The number of arguments passed to the script
$*
Displays all parameters passed to the script in a single string
$$
The current process ID number for the script to run
$!
ID number of the last process running in the background
, The interpreter (interpreter) needs to read the source code that we wrote and convert it to the target (object), which is then run by the computer. Because each execution of the program is more than the compilation process, so efficiency has decreased.The advantage of using scripting languages is that they are mostly run at a higher level than the compiled language and can easily handle objects such as files and directories, and the disadvantage is
specified shell command
V
Start the VI editor with the current file
.
Repeat the previous command
N
Find in file the overflow matches the content of the specified expression
‘
Jumps to the first content that the specified expression matches to
Ctrl+l
Redraw the current scre
, The interpreter (interpreter) needs to read the source code that we wrote and convert it to the target (object), which is then run by the computer. Because each execution of the program is more than the compilation process, so efficiency has decreased.The advantage of using scripting languages is that they are mostly run at a higher level than the compiled language and can easily handle objects such as files and directories, and the disadvantage is
True when the length of a string is greater than 0
-Z str1
True when the length of a string is 0
Str
True when the string is non-empty
As an example, compare strings to test the user id:If ["$ (whoami)"! = ' root ']; Thenecho "You had no permission to run $ as Non-root user."Exit 1;Fi
Numeric comparison (integer)
operator
Compare Results
Num1-eq num2
Two numbers equal to True
used for [[]], be aware that there are spaces on both sides of the logical expression and on both sides of the comparison operator. If Used (()), you do not have to leave a blank. Mathematical comparison operators are not available in addition to = and!=,[]. in [[]], two types of numeric comparison operators are available. You can only use =,! =, File judgment and comparison logical expressions-e file exists-F normal file (not directory or device file)-S is not empty file-D file is directory-H
from the Code Farm supplement Linus and Linux, Chapter: Shell you may not know. recently finished reading this book, feel that there are a lot of good content, this is one of the shell small dessert, bring to share with you, add some of my own test content. The writer writes this essay to share and improve everyone's interest in the shell. Read the following
Find a very detailed Linux shell script tutorial, in fact, not only the shell script programming, but also introduced the system of various commandsHttp://vdisk.weibo.com/s/yVBlEojGMQMpvThis book is divided into five parts, in detail the shell programming skills, a variety of UNIX commands and syntax, but also related
, convert uppercase characters to lowercase. option allows you to specify the characters you want to delete, and to condense the inherited characters into one.Common options:-C take Source-char-list's inverse meaning. TR the character to be converted into a character that is not listed in the source-char-list. This option is typically used in conjunction with-D or-S.-C is similar to-C, but handles characters rather than binary byte values.-D self-standard input removes the characters listed in S
Getting Started with Shell scripting--NextThis article introduces the following: Logical operators (, | 、!、 ^), test commands, script execution procedures1. Logical operators(1) and short circuit andTrue, False1 0And:1 1 = 11 0 = 00 1 = 00 0 = 0For , if the left condition is false, the value of the right condition is also evaluated. Features as long as a false is assumed to be falseShort Circuit with:
=" wkiom1m89gsqup5laaayxllznls535.png "/> Execution Result: (believe that you know everything)650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/A5/69/wKioL1m89FHR3lnMAAArUD8lCS4739.png "title=" 99. PNG "alt=" Wkiol1m89fhr3lnmaaarud8lcs4739.png "/>Jump out of a single loop Continu statement:Continue [n]: End of the nth layer of the current cycle, and directly into the next round of judgment; the inner layer is the 1th floor.Come on, let's take an example:650) this.width=650; "src=" Http
Core:------again---again!How ops people learn shell scriptingOld boy Linux early graduate students talk about how to learn shell programming skillsAfter graduating from school, he served as operations manager for a nearly thousand company. Currently employed Xiaomi Technology1. Basic grammar, knocking n+1 all over. Why not n times that, because N times is the first few days you've just started working on pr
This is the first phase of the shell Learning Series and will be constantly updated...
Specific Learning books: Unix shell programming (Third edition)
This section describes some tools that are frequently used in shell programming.
1. Shell tool ---- Regular Expression
For
Tags: rectangle exp Specify nohup How many exit sed take out rowThis article mainly introduces examples of shell programming First, the logical judgment if statement 1. Judging the age?[[emailprotected] 9_1 ]#cat iftest.sh
#!/bin/bash
read -p "Please input your age:" age
## 判断用户输入的必须是数字
if [[ "$age" =~ ^[0-9]+$ ]];then
true
else
echo "Please input digit"
exit 10
fi
## 判断用户的年龄,并输出相应的信息
if
First, the basicView all shellsCat/etc/shells or Chsh–l1. Examples of scripts.Linux does not differentiate files, for the convenience of memory, here with. Sh end.(1) Vim first.sh#!/bin/bashMkdir/root/shellIfconfig(2) Add Execute permissions chmod +x first.sh The file will turn green at this point.(3) Execution: 5 ways:①./first.sh② Absolute Path execution③sh + script name (no execute permission required)④source + script name (does not require EXECUTE
How can shell scripting be used better? Especially in UNIX and Linux operating systems. Next, let's take a look at how to use shell script programming.
Like other UNIX operating systems and Linux systems, IBM? AIX? The operating system provides several powerful tools to help
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.