Shell basics 3: shell Basics

Source: Internet
Author: User
Tags print format

Shell basics 3: shell Basics

Cut is a selection command:

It is to analyze a piece of data and retrieve what we want. In general, the selection information is usually analyzed for "rows", not the entire information analysis.

(1) The syntax format is:

Cut [-bn] [file] or cut [-c] [file] or cut [-df] [file]

Instructions for use
The cut command cut bytes, characters, and fields from each line of the file and writes these bytes, characters, and fields to the standard output.
If the File parameter is not specified, the cut command reads the standard input. -B,-c, or-f must be specified.

Main Parameters
-B: Split in bytes. These byte locations ignore the multi-byte character boundary unless the-n flag is also specified.
-C: separated by characters.
-D: custom delimiter. The default Delimiter is tab.
-F: used with-d to specify the region to display.
-N: undelimiter multi-byte characters. It is used only with the-B flag. If the last byte of a character falls within the range indicated by the-B mark List parameter, 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/etc/passwd file on my Unix system. I use this file because fields in the file are separated by the ":" character, which make it very easy to work.

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

cut -f1 -d: /etc/passwd

This cut command can be read:

Print the first field (-f1)
Fields are delimited by the ":" character (-d :)
Use the/etc/passwd file as input
The output of this command will vary by Unix and Linux systems, but it will be the first field of your/etc/passwd file, which contains the name of the users on your system. this will look something like:

nobodyalvingeorgefred
#! /Bin/bash # program: test example 01 PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /binexport PATHfor value in dog "cat" 'bird '# note that no special do echo "animal are $ {value} s" donefuhui @ ubuntu: ~ is found in quotation marks :~ /Script $ sh sh22.sh animal are dogsanimal are catsanimal are birds

Add sudo permissions to users in linux
1. Log On with the root account or su to the root account.

2. Add the write permission for the sudoers file: chmod u + w/etc/sudoers

3. Find root ALL = (ALL) ALL in vim/etc/sudoers and add dituhui ALL = (ALL) ALL (ps: dituhui stands for the user name you want to add sudo permissions) under this line)

4. Except the sudoers file write permission: chmod u-w/etc/sudoers

My account information under passwd

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

Command: id Function Description: displays the uid, gid, group, and user name of the current Logon account.

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 user information. The user name, Home Directory, stagnation time, Logon Time, and logon shell information of a user in the system are usually displayed. To query user information on a remote machine, you need to connect the user name to "@ host name" in the format of [user name @ host name]. However, the network host to be queried must run the finger daemon.

Login: fuhui                            Name: FirstLinuxDirectory: /home/fuhui                  Shell: /bin/bashOn since Sun Jun 21 18:22 (PDT) on pts/12 from 192.168.187.1   7 seconds idleNo mail.No Plan.
#! /Bin/bash # program: test example 01 PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /binexport PATHusers = $ (cut-f1-d:/etc/passwd) for name in $ usersdo id $ name # Note: directly execute the command statement finger $ namedone

More detailed usage of linux seq

The seq command is used to print a string of ordered numbers, seq (sequence of number ).

It consists of the following three parameters:

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

-F: Specifies the print format:
For example:

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

-S specifies that the separator is the carriage return by default (-s can be followed by no quotation marks ):
For example:

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

-W outputs are supplemented with "0" when the front of the same width is insufficient, that is, they are aligned with the maximum number of digits.
For example:

[root@hao32]# seq -w 2 1102030405060708091011

/Dev/null 2> & 1

First, you need to understand the input and output streams of unix systems. Their correspondence with numbers is as follows:

0: standard input stream (stdin) 1: Standard output stream (stdout) 2: Standard Error stream (stderr)

SO 2> & 1 means to redirect stderr to stdout and display it together on the screen. If no number is added, the default redirection is for stdout.

Statement:/dev/null indicates a null device file;> indicates redirection; 2 indicates a standard error stream; & indicates reference, which means the same. Therefore, the statement means to redirect the standard output and standard error output to an empty device file to shield the information from being displayed.

#! /Bin/bash # program: test example 01 PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /binexport PATHnetwork = '2017. 168.1 'for sitenu in $ (seq 1 10) do # note that $ {sitenu} and $ {sitenu} are completely different ping-c 1-w 1 "$ {network }. $ {sitenu} ">/dev/null & result = 0 | result = 1 # Note>/dev/null, no use of-w found. if ["$ result" = 0]; then echo "$ {network }. $ {sitenu} is up "else echo" $ {network }. $ {sitenu} is down "fidone

The difference between the value of a variable and the value of a variable is whether the prefix $

Ls-lh $ dir>/dev/null if it is written in this way, it is finished and no output! -D $ dir: Check whether the directory exists.
#!/bin/bash# program:test example 01PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binexport PATHread -p "please input a directory:" dirif [ $dir = '' -o ! -d $dir ]; then        echo "the $dir is not existed in the system"        exit 1fifor info in $( ls $dir)do        perm=''        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
Another input method of the for Loop is for (initial value; limit value; step size) do program segment done
#! /Bin/bash # program: test example 01 PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /binexport PATHread-p "please input a number:" nudeclare-I sum = 0for (I = 1; I <= $ nu; I = I + 1) # note, $ ido sum =$ ($ sum + $ I) doneecho "the total is: $ sum" is not used here to test the addition of $ #! /Bin/bash # program: test example 01 PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin: /binexport PATHread-p "please input a number:" nudeclare-I sum = 0for (I = 1; $ I <= $ nu; I = $ I + 1 )) # changed to a general understanding and found that it is still acceptable. I feel that this is more in line with the general logic do sum = $ ($ sum + $ I) doneecho "the total is: $ sum "the following error should not be written in $ I. I don't know where the syntax is wrong. fuhui @ ubuntu :~ /Script $ sh-n sh27.sh sh27.sh: 9: sh27.sh: Syntax error: Bad for loop variable

Check whether the script has a syntax error.

[root@www ~]# sh [-nvx] scripts.sh  

Option parameters:

-N: If no script is executed, only query whether the syntax is faulty.-v: Before executing scloud, output the content of scripts to the screen.-x: displays the script content on the screen. This is a useful parameter!
fuhui@ubuntu:~/script$ sh -x sh27.sh + PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin+ export PATH+ read -p please input a number: nuplease input a number:

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.