Shell Script Basic Commands

Source: Internet
Author: User

How shell scripts run
$sh script.sh$sh/home/path/script.sh$chmod a+x script.sh; $./script.sh

Echo's three ways to use echo Hello World Cannot contain some special keywords, such as semicolons;
echo "Hello word" Cannot contain some special characters, such as!
echo ' Hello word ' Variables containing ' $var ' cannot be evaluated


Echo appends a newline character to the output by default, and if you want to disable it, you can use the-n option
If you want to make some escape characters effective, you can use the-e option, such as
echo "\TA\TB" will output \TA\TB
Echo-e "\TA\TB" will output a B


Show environment variables for some processes
[Email protected] ~]$ pgrep bash3182[[email protected] ~]$ Cat/proc/3182/environ user=hadooplogname=hadoophome=/home/ hadooppath=/usr/local/bin:/bin:/usr/binmail=/var/mail/hadoopshell=/bin/bashssh_client=192.168.1.1 5649 22SSH_ connection=192.168.1.1 5649 192.168.1.10 22ssh_tty=/dev/pts/0term=vt100selinux_role_requested=selinux_level_ Requested=selinux_use_current_range=



Arithmetic operations

Use Let, (()), [] to perform basic arithmetic operations.

such as let A = b+c; Let A++;let a--;
You can also use [], a=$[b+c]; a=$[$b +5]
You can also use (()), but you need to add $ to the variable before
Delete variable: unset


Tee

Tee command: Use stdin as input, enter it into a file, and output it as-is



Show variables

Set displays variables for the current shell, including current user environment variables and their own defined variables

env Display current User environment variables
Export Displays the shell variables currently exported as user variables


Associative arrays that is MAP
[[email protected] ~]$ declare-a map_array[[email protected] ~]$ map_array[index1]=val1[[email protected] ~]$ map_array[ Index2]=val2[[email protected] ~]$ echo ${map_array[*]}val1 val2[[email protected] ~]$ echo ${!map_array[*]}index1 Index2

Cat function: Read, display, and splice file contents.
An output of ab two file contents [[email protected] ~]$ cat A B
Display the input text with a piece [[email protected] ~]$ echo "Hello" | Cat-a
Compress blank line cat-s A
Show Tabs as ^| Cat-t A
Display text line number Cat-n A


Find function: Find all files and folders under path and sub-path
Find all files in the script directory
[Email protected] ~]$ Find Script/script/script/timing.logscript/output.session


Match Search by file name or regular expression
[Email protected] ~]$ find script/-name "t*" #必须用引号括起来 [[email protected] ~]$ find script/-iname "t*" #忽略文件名的大小写


Multiple conditions can be connected with or
[[email protected] ~]$ find script/\ (-name "t*"-o-name "out*" \) #注意空格script/timing.logscript/output.session


You can use parameters to specify the lookup depth
-maxdepth 1 #指定查找的最大深度

-mindepth 1 #指定查找的最小深度, which lists only a list of files over 1 layers


Search by File type

-type d #只列出文件夹-type F #只列出普通文件


You can also use-delete to delete found files, depending on the time and file size.
-exec can process each of the files identified, such as changing the file owner root to Hadoop:
[[email protected] temp]$ sudo find. -type f-user root-exec chown Hadoop {} \;

Xargs function: Converts a standard input into a command-line parameter or converts a single-line or multiline text to a command-line argument
[email protected] ~]$ Cat A | Xargs #将A文件多行数据转换成单行输入, replacing newline characters with spaces [[email protected] ~]$ Cat A | Xargs-n 3# re-split the multi-line data of A file and group by 3 [[email protected] ~]$ Cat A | xargs-d I #指定分隔符为 I

Tip: Xargs default is to split the record with whitespace characters (spaces, tabs, line breaks), so file 1.log is interpreted as two records file and 1.log, unfortunately RM cannot find these two files. In order to solve such problems, let the Find command print out the A file name is then output a null character ("') instead of a newline character, and then tells Xargs to use the null character as the delimiter for the record. This is the origin of the-print0 and Xargs 0 of find.


Sort and Uniq
[[Email protected] ~] $sort A #对A文件进行升序排序 [[email protected] ~] $sort-R a #对A文件进行逆序排序 [[email protected] ~] $sort-N a #对A文件按数 The word is sorted [[email protected] ~] $sort-K 2 A #根据第二列进行排序


Uniq is always used with sort to eliminate duplicate rows

Shell Script Basic Commands

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.