Common Lunix shell Basics

Source: Internet
Author: User
Tags lunix
1 PS-EF: displays running processes, PID, and other information
Uid pid ppid C stime tty time cmdroot 1 0 0 03:45? 00:00:02 init [5] Root 2 1 0? 00:00:00 [migration/0] root 3 1 0 0:45? 00:00:00 [ksoftirqd/0] root 4 1 0? 00:00:00 [events/0] root 5 1 0? 00:00:00 [khelper] root 6 1 0? 00:00:00 [kthread] root 9 6 0? 00:00:00 [kblockd/0] root 10 6 0? 00:00:00 [kacpid]

2 DF-H readable display disk space M is MB filesystem size used avail use % mounted on/dev/sda2 18g 5.0g 12g 31%/dev/sda1 289 M 16 m 258 M 6%/boottmpfs 506 M 0 506 m 0%/dev/SHM
3. Using the DF command, you can easily check whether the disk space is insufficient. The system administrator needs to know what to do if the disk space is insufficient. We can use the du command to display the disk usage in a specific directory. This is a quick way to determine whether the system has a large disk footprint. Du, Du-H (display M, G unit), Du-C (Summary), Du-s (Summary all, show only one) du [-A |-S] [-K] [-M] [-G] [-L] [-R] [-x] [-H |-L] [file...]
8.0 K./test_flume/. flumespool24k./test_flume8.0k./mnt58m./awk28k./shell18m./spark76m.
4 sort (SORT) Sort file.txt
By default, sort-N file.txt sort-N file2023567917 is sorted by characters.
Sort-M file3.txt (sort by month)
Sort-t': '-K 3-N/etc/passwd sort by specified delimiters': "The third field is sorted by number
Du-sh * | sort-Nr sort by space usage in descending order, including folders and files.
5. gzip: Used to compress files
Gzip 2. shgzip file * gzip-r test1 Test2 compress 2 files

Tar-CVF my.tar my. Sh tar compression tar-xvf my. Tar Decompression
Tar-zxvf filename. tgz
7. Print the environment variable printenv.
Bashtest = testingecho $ Test
7 finger llisc
/Usr/sbin/groupadd shared
/Usr/sbin/usermod-G shared Test
File Permission table 755



Chmod o + R newfile Add read entries to anyone
Chmod U-x newfile: the chmod u + x newfile
Chown: Modify the owner and group of a User File

9 Shell
Rpm-Qa | sort | more
Echo $?




If expression returns 0 successfully executed thenfi

If test [condition]

For VaR in li72 Alibaba Taobao newbatilrdo echo the state $ testdone

Use escape character backslash to transfer single quotes use double quotation marks to define single quotes

#! /Bin/bashfor file in/home/bigdata/test/*/home/li75/do if [-d "$ file"] Then ECHO "$ file is a directory" Elif [-F "$ file"] Then ECHO "$ file is a file" fidone



#! /Bin/bashfor (I = 1; I <= 10; I ++) do echo "the next number is $ I" done

#! /Bin/bash var1 = 10 while [$ var1-GT 0] Do echo $ var1 var1 = $ [$ var1-1] Done

#! /Bin/bashvar1 = 100 until [$ var1-EQ 0] Do echo $ var1 var1 = $ [$ var1-25] Done

#! /Bin/bashfor (a = 1; A <= 3; A ++) do echo "Starting loop $ A:" For (B = 1; B <= 3; B ++) do echo "inside loop: $ A * $ B" donedone

Breakcontince

#! /Bin/bashfor (a = 1; A <4; A ++) do echo "outer loop: $ A" for (B = 1; B <100; B ++) do if [$ B-GT 4] Then break 2 fiecho "Inner Loop: $ B" donedone> test.txt redirects to a file | sort by sort

Parameter location parameter $0 is the program name $1 is the first parameter $2 is the second parameter $3 is the third parameter

#! /Bin/bashfactorial = 1for (number = 1; number <= $1; number ++ )) do factorial = $ [$ factorial * $ number] doneecho the factorial of $1 is $ factorial


Name = 'basename $ 0' get the name of the running shell script


If [-n "$1"] determines whether a parameter is input.
If [$ # <5] determines the number of input parameters
Get the value of the last parameter Params = #
Echo parm: $ Params or the last paramer $ {! #}

$ * Treat all parameters as one [email protected] and treat all parameters separately as strings

#! /Bin/bashwhile [-n "$1"] Do case "$1" in-a) echo "found the-A option";-B) echo "found the-B Option";-C) echo "found the-C option"; *) echo "$1 is not an option"; esac shiftdone




#! /Bin/bashecho-n' enter your name: "read nameecho" Hello $ name, welcome to my prorram ."

#! /Bin/bashread-P "Please enter your age:" agedays = $ [$ age * 365] echo "that makes you over $ days old! "
Ls-Al test test22 test3 badtest &> test7
#! /Bin/bashecho "this is an error"> & 2 Echo "this is normal output"
#! /Bin/bashexec 1> testoutecho "this is a test of redirting all output" Echo "without having to redirect every line"
#! /Bin/bashexec 2> testerror
Echo "this is the start of the script" Echo "Now reidirecting all output to another" Exec 1> testout2
Echo "This putput shocould go to the" Echo "But shis shocould go to testerror File> & 2


#! /Bin/bashexec 0 <testfile2count = 1 while read linedo echo "line # $ count: $ line" Count = $ [$ count + 1] Done
Lsof-a-p $-D 0, 1, 2
Redirect to an empty file LS-Al>/dev/null
CAT/dev/null> testfile
Simultaneously record and print files to the screen
Date | tee testfile
Append data
Date | tee-A testfile
Kill-9 process ID


#! /Bin/bashtrap "Echo haha" SIGINT sigteermecho "this is a test program" Count = 1 while [$ count-le 10] Do echo "loop # $ count" sleep 10 COUNT = $ [$ count + 1] Done echo "this is the end of test program"

Run the command in the background &
Sh 37.sh &
PS Au
Sometimes you need to start the session from the terminal, and then let the script run in later mode before the end, even if you exit the terminal session, sometimes nohup can be used
Jobsjobs-rjobs-sjobs-l
Nice set priority
Nice-N 10 sh 38.sh> test4out &


At command batch command cron table

#! /Bin/bashtime = 'date + % t' echo "this script ran at $ time" Echo "this is the end of the script"> & 2

The corn table uses a special format to specify the job time.
15 10 *** command
View the scheduled tasks configured by the Login User
Crontab-l
Edit task corntab-e

#! /Bin/bashfunction func1 {echo 'this is an example of a function "} COUNT = 1 while [$ count-le 5] Do func1 COUNT = $ [$ count + 1] Done echo "this is the end of the loop" func1 echo "Now this is the end of the script"
Function return value $?
#! /Bin/bashfunc1 () {echo "trying to display a non" ls-ls 41.sh} echo "testing the function:" func1echo "the exit status is: $? "
#! /Bin/bashfunction db1 {read-P "enter a value:" value echo "doubling the value" return $ [$ value * 2]} db1 echo "the new value is $? "

Function return value
#! /Bin/bashfunction db1 {read-P "enter a value:" valueecho $ [$ value * 2]}
Result = 'db1' echo the new value is $ result"

To provide function files, you can use the source command or the dot operator to import existing library functions to. bashrc.
Source/home/li72/libraries/myfuncs./home/li72/libraries/myfuncs

Function addem {echo $[$1 + $2]}
Function multem {echo $[$1 * $2]}
Function divem {if [$2-Ne 0] Then ECHO $ [$1/$2] else echo-1fi}

#! /Bin/bash ../myfuncs
Result = 'addem 10 16' echo "The result is $ result"
. Bashrc checks the file every time shell is started, and adds a user-defined function to the end of an existing file.
./Home/bigdata/test/Shell/myfuncs
Text processing sed luw.edit and replace
Gawk

Echo "this is a test" | SED's/test/big test /'
Cat> dog.txt The quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog. the quick brown fox jumps over the lazy dog.
Sed's/dog/CAT/'dog.txt The quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat. the quick brown fox jumps over the lazy cat.

Sed-e '> S/brown/green/> S/Fox/elephant/> S/dog/CAT/'dog.txt
The quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat.
Read the editor command from the file cat> scripts/brown/green/S/Fox/elephant/S/dog/CAT/

Sed-F script dog.txt
The quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat. the quick green elephant jumps over the lazy cat.

Cat> script2 {print $5 "'s userid is" $1}
Add User
Useradd Xuhui Change Password usermod-P Xuhui
View the password file tail-L/etc/shadow

CD-return the last accessed directory
File SEARCH Command
Find [path] [Option] [operation]



Find.-Name 'test *'

Find.-mtime-90-print: searches for files whose change time is within 90 days.
Grep [Option] [mode] [file]


Find the world word grep-w'world' wodl.txt in wodl.txt.


















Common Lunix shell Basics

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.