Linux Shell Scripts

Source: Internet
Author: User
Tags square root

#! /bin/bash

Shebang is a line of text where #! Before the interpreter path,/bin/bash is the Interpreter command path for bash.

There are two ways to run a script, one is to use the script as a command-line argument for bash, and the other is to grant script execution permission to make it executable.

To set executable permissions on a script:

$bash script. SH #文件名称 $ chmod 777 script. sh# Grant Read and write executable permissions to files
$./script. Sh#./represents the current directory

    1. Using the shell for mathematical operations

The Let command can perform mathematical operations directly, and the variable name does not need to be added before the $

#!/bin/bashnum1=5num2=6Letresult=num1+num2Echo $ Result

Self-adding operation
Let num1++
Self-decrement operation
Let num1--
Abbreviated form
Let num1+=5
Let num1-=5
is equivalent to
Num1=num1+5
Num1=num1-5

Other methods

operator [], expr can also be implemented like let

RESULT=$[NUM1+NUM2]
result=$[$num 1+5]

Expr arithmetic operations and string http://www.cnblogs.com/kex1n/p/6605376.html

Example 1:#! /bin/bashnum1=5num2=6result1=$ (expr $num 1+$num 2) RESULR2=$ (expr $num 1 + $num 2)echo  $result 1echo $result 2
Output: The result of RESULT1 is 5+6
The result of RESULT2 is 11.
Why is that?
Operators have spaces around them, and if no spaces indicate a string connection

These operations can only be integer operations and do not support floating-point

2.BC can use it to perform floating-point operations

echo "4 * 5.5" | Bc

 #!/bin/bashnum1  =4  num2  =5  result  = ' Span style= "COLOR: #0000ff" >echo   "  $num 1 * $ num2   " |  BC '  echo  $result 
set the decimal exact number of digits Echo "Scale=2;38" | BC Conversion num=echo "obase=2; $num" | BC Square root Echo "sqrt | bc

3. Output text redirection or save text to a different file

Example 1
Echo "My frist Progress" >Text.ext
Example 2echo "My frist Progress" >>text.ext

Example 1: The contents of the Text.ext file are emptied first and then written to my frist progress

Example 2: Continue writing to my frist at the end of the Text.ext file content progress

4. Arrays and associative arrays

when row array Array_var= (0123456)
Define array values as a set of "index-values"
array_var[0]= "Text1"
array_var[1]= "Text2"
array_var[2]= "Text3"
array_var[3]= "Text4"

array_var[5]= "Text6"
Print specific array values
echo ${array_var[1]} #打印某一个数组
Num=5
echo ${array_var[num]} #打印某一个数组
echo ${array_var[*]} #打印所有数组
echo ${#array_var [*]} #打印数组长度
-A ass_arrayass_array={[apple]= ' [banana]= ' }echo  "Apple  costs is ${ass_array[apple]}"
Echo 10
Banana Apple

Linux Shell Scripts

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.