Bashell basics, bashell

Source: Internet
Author: User
Tags unzip archive

Bashell basics, bashell

As an iOS programmer, although iOS-related technologies are very important, bash is also unknown, because the growth of skills requires both depth and breadth. Next we will introduce bash.

Shell is written in C language, so it is an explanatory language. It runs in Linux and on Mac, and generally uses bashell.

Bashell is running under Terminal, and all scripts in this article are also running under Terminal.

Common command lines
Pwd // current path ls-l // list all files in the current directory mkdir TestFolder // create a folder touch file.txt file1.txt file2.txt // create three files, space separation ls-l | grep txt | wc-l // list all files, search for files whose names contain txt, and calculate the total number of results

Note: | the result on the left is used as the input on the right.

Man command // The man command is followed by a command to display all the help information of the command, then input q to return history // display all the previously entered historical information clear // clear all the lines du file // display the file size cat file // display the file content less file // pagination show file content, suitable for copying a new file from a large file cp file to a new file mv file directory // move the file to the directory rm file 1 file 2 file 3 // delete file 1, file 2, file 3rm-rf/* or rm-rf // delete all files. Do not execute this sentence !!!!! Never !!!! Sudo su // become the root user chomd + x file // execute permissions on the file, which is very useful in shell.
Wc-m <END // reads data from the keyboard and counts the size of the read data, END with the END symbol ps-ef // display the list of all processes top // display the process dynamic list kill process PID // END a process tar-cvf archive.tar file1.txt file2.txt file3.txt // archive tar -xvf tutorial.tar // unarchive gzip tutorial.tar // compress gunzip tutorial.tar.gz // decompress unzip archive.zip // decompress

The basic command line is introduced here, and shell is started below

Shell Basics

Before introducing shell, you must first understand vim

Enter vim and then enter the operation interface. There are three modes: Interactive, plug-in, and imperative. Specifically, I personally think the interactive and imperative types are close to each other. They are all input using commands. The difference is that the imperative type needs to be input first:

Enter

Vim file // generally ends with. sh. You can end with txt and press I to enter the insert mode. Then you can start writing shell.

Let's enter some code to talk about the basic shell syntax separately:

# Specify the interpreter for running shell #! /Bin/bash # output text echo "your name" # format input text printf "% d % s" 123 "abcds" # input read PERSON from keyboard # print input content echo "hello $ PERSON" # val is a variable, expr is an operator. The reverse quotation marks indicate that val can first store the content in the memory, and then read the result from the memory. There cannot be spaces around the equal sign, the space behind the expr cannot be less val = 'expr 2 + 2' # $ reference variable. Double quotation marks and single quotation marks are used in the same way. However, if a variable is contained, use double quotation marks echo "value: $ val "# if syntax, the space cannot be less; eq indicates equal, if [10-eq 100] then echo "equal" else echo "not equal" fi file = "test. sh "#-f indicates whether a common file if [-f $ file] then echo" Ordinary file "else echo" not ordinary file "fistring =" 79838924 kfdsldjslfjdslfs "# string Length echo $ {# string} # string 1st to 5 bits, output 98389 echo $ {string: 1: 5} name [0] = "a" name [1] = "B" name [2] = "c" # print the echo $ {name [*]} Member of the array name length = $ {# name [*]} # print the array length echo "array length is $ length" # for syntax ,. /* indicates all files in the current directory. mm indicates a single file in the loop body for mm in. /* do echo $ mmdone count = 0 # while syntax structure,-lt indicates less thanwhile [$ count-lt 5] do count = 'expr $ count + 1' ec Ho $ countdone # case syntax structure aNum = 1 case $ aNum in 1) echo 'abc'; *) echo 'default'; esac # shell function, with parameter syntax, $1 indicates the first parameter, $2 indicates the second # note. $10 cannot obtain the tenth parameter, and $ {10} is required to obtain the tenth parameter }. When n> = 10, use $ {n} to obtain the functionWithParam () parameter () {echo "first param $1" echo "second param $2" return 123} # function parameter functionWithParam 1 2 # Get function return value ret =$? Echo $ ret # $ # number of parameters passed to the function # $ * display all parameters passed to the function # first parameter option for receiving. sh = "$ {1 }"

After all these scripts are input, Press esc and enter wq to save and exit vim.

Then enter:

Chmod + x test. sh // give the sh file the execution permission

. /Test. sh // start executing the script. /Is the execution PATH. If your script is placed in a PATH such as/bin,/sbin,/usr/bin,/usr/sbin, it can be omitted./

If you want to perform the debugging, enter:

Bash-x test. sh

 

Here is an introduction to basic command lines and basic bash.

 

References:

Http://www.jianshu.com/p/7c3a4f56f671

Http://c.biancheng.net/cpp/view/6994.html

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.