Shell Basics 3

Source: Internet
Author: User
Tags shebang

Cut is a selection command:

is to analyze a piece of data and take out what we want. In general, the selection of information is usually for "line" to analyze, not the entire information analysis.

(1) The syntax format is:

cut  [-bn] [file] 或 cut [-c] [file]  或  cut [-df] [file]

Instructions for use
The cut command cuts bytes, characters, and fields from each line of the file and writes those bytes, characters, and fields to standard output.
If you do not specify a File parameter, the Cut command reads standard input. One of the-B,-C, or-f flags must be specified.

Main parameters
-B: Split in bytes. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.
-C: Split in characters.
-D: Custom delimiter, default is tab.
-F: Used with-D to specify which area to display.
-N: Cancels splitting multibyte characters. Used only with the-B flag. If the last byte of a character falls within the range indicated by the List parameter of the-B flag, the character is written out; otherwise, the character is excluded.

Use of cut

Using The Cut command with/etc/passwd
For a First Cut command example, I'll use the the/etc/passwd file on my Unix system. I Use this file because the file is separated by the ': ' character, which make it very easy-to-work with.

Using that file, here's a Linux cut command that prints the first field (first column) of the every line in this file:

-f-d: /etc/passwd

This cut command can is read as:

Print the first field (-F1)
Fields is delimited by the ":" Character (-D:)
Use the/etc/passwd file as input
The output of this command would vary by Unix and Linux systems, but it would be the first field of YOUR/ETC/PASSWD file, W Hich contains the name of the users on your system. This would look something like:

nobodyalvingeorgefred
#!/bin/bash# program:test ExamplePATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binExportPATH forValueinchDog"Cat" ' Bird '   #注意 quotes didn't find anything special. DoEcho"Animal is ${value}s"Donefuhui@ubuntu: ~/script$ SH sh22.sh animal is dogsanimal is catsanimal is birds

Linux adds sudo permissions to the user
1, log in with root account or Su to root.

2, increase the Write permission of sudoers file: chmod u+w/etc/sudoers

3, Vim/etc/sudoers find root all= (all) all in this line add Dituhui all= (All) All (Ps:dituhui represents the user name you want to add sudo permissions)

4, remove the sudoers file Write permission: chmod u-w/etc/sudoers

passwd My Account information

fuhui:x:1000:1000:FirstLinux,,,:/home/fuhui:/bin/bash

Command: ID Function Description: View displays the UID and GID of the current login account and the group and user name

fuhui@ubuntu:~/script$ id fuhuiuid=1000(fuhui) gid=1000(fuhui) groups=1000(fuhui),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)

Command: Finger

Function Description: Query the user's information, usually displays the user name of a user in the system, home directory, stasis time, logon time, login shell and other information. If you want to query the user information on the remote computer, you need to follow the user name "@ hostname", the [username @ hostname] format, but to query the network host needs to run the finger daemon.

login:fuhui name:firstlinuxdirectory :/home/fuhui Shell:/bin/bashon since  Sun June 21  18 : 22  (PDT) on   pts/12  from  192.168  .187  Span class= "Hljs-number" >.1  7  seconds Idleno Mail. No Plan.  
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binPATHusers=$(cut -f1 -d: /etc/passwd)forin$usersdo        $name   #注意,直接执行命令语句        $namedone

More detailed usage of Linux seq

The purpose of the SEQ command is to print out a sequence of sequential numbers, seq (sequence of number).

It consists of the following 3 parameters:

       -f, --format=FORMAT           use printf style floating-point FORMAT (default: %g)

-f Specifies the format for printing:
For example:

[root@hao32]# seq -f %05g 2 7 000020000300004000050000600007
 -s, --separator=STRING          use STRING to separate numbers (default: \n)

-s Specifies that the delimiter is carriage return by default (-S can be followed without quotation marks):
For example:

[root@hao32]# seq -s" " 2 7     234567
   -w, --equal-width          equalize width by padding with leading zeroes

-W output is the same width front insufficient with "0" completion, that is, with the number of digits aligned
For example:

[root@hao32]# seq -w 2 1102030405060708091011

The role of/dev/null 2>&1

The first thing you need to know about the input and output streams of UNIX systems is that they correspond to numbers separately:

    0:标准输入流( stdin )    1 : 标准输出流( stdout )    2 : 标准错误流( stderr )

So 2>&1 means redirecting stderr to stdout and showing it together on the screen. If you do not add a number, the default redirection is for stdout.

Statement:/dev/null represents an empty device file; > represents a redirect; 2 represents a standard error stream; & represents a reference, an equivalent meaning. So the statement means that the standard output and the standard error output are redirected to the empty device file, and the information screen is not displayed.

#!/bin/bash# program:test ExamplePath=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binExportpathnetwork=' 192.168.1 ' forSitenuinch$ (SEQ1 Ten) Do      #注意 ${sitenu} and $${sitenu} are completely different.Ping-c1-W1 "${network}. ${sitenu}">/dev/null && result=0|| result=1  #注意 >/dev/null, did not find the use of-W        if["$result"=0]; Then                Echo  "${network}. ${sitenu}  is up "        Else                Echo "${network}. ${sitenu}  is down "        fi Done

The difference between assigning a value with a variable and a variable is whether there is a prefix $

ls -lh $dir > /dev/null 如果是这样写的话,完了,没有输出! -d $dir 判断目录是否存在,注意写法
#!/bin/bash# program:test ExamplePath=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binExportPATHRead-P"Please input a directory:"Dirif[$dir="'-O!- D $dir]; Then        Echo "The $dir is not existed in the system"        Exit 1fi forInfoinch$ (LS$dir) Doperm="'Test-r"$dir/$info"&& perm="$perm readable"Test-w"$dir/$info"&& perm="$perm writeable"Test-x"$dir/$info"&& perm="$perm executable"        Echo "The $dir/$info permission $perm" Done
for循环的另一种输入方式for (( 初始值; 限定值; 步长 ))do    程序段done
#!/bin/bash# program:test ExamplePATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binExportPATHRead-p"Please input a number:"Nudeclare-i sum=0 for((i=1; i<=$nu; i=i+1))#注意, $i is not used here. Dosum=$(($sum+$i)) Doneecho"The total is: $sum"Here the test Plus$#!/bin/bash# program:test ExamplePATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binExportPATHRead-p"Please input a number:"Nudeclare-i sum=0 for((i=1;$i<=$nu; I=$i+1))#修改成了一般的认识, found still able to feel this more in line with the usual logic Dosum=$(($sum+$i)) Doneecho"The total is: $sum"The following error should not be$iI don't know where the syntax is wrong Fuhui@ubuntu: ~/script$ Sh-n sh27.sh sh27.SH: 9:Sh27.SH: Syntax Error:  Bad  forLoop variable

To determine if a script has a syntax error

[[email protected] ~]# sh [-nvx] scripts.sh  

Option parameters:

-n :不执行script,仅查询语法是否存在问题;-v :再执行 sccript 前,先将 scripts 的内容输出到屏幕上;-x :将使用到的 script 内容显示到屏幕上,这是很有用的参数!
fuhui@ubuntu:~/script$ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binPATHnumber:number:

Shell Basics 3

Related Article

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.