Linux Shell Example (i)

Source: Internet
Author: User

First, the environment
System: Centos6.6 x64
Shell:bash, SH

[[email protected] ~]$ hostnameShell[[email protected] ~]$ lsb_release -aLSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarchDistributor ID: CentOSDescription:    CentOS release 6.6 (Final)Release:    6.6Codename:   Final[[email protected] ~]$ echo $SHELL/bin/bash[[email protected] ~]$ ifconfig eth0 |grep "t addr"| awk -F ‘[: ]+‘ ‘{print $4}‘192.168.101.110

Ii. Examples of scripts
1, Terminal printing

#!/bin/bash#The printf of Linux termnal,Include "echo,printf"#author by woon echo "The shell\`s name is $0"#examples of echo,printf#默认打印。输出换行echo "Hello World!" echo ‘Hello World!‘#echo -n参数接受不换行输出echo -n "Hello World"echo "Hello World"#-e参数接受双引号内字符串的转移列表echo "Hello\tWorld!"echo -e "Hello\tWorld!"#printf使用文本或由空格分割的参数,可指定输出的宽度、对其方式等,可以格式化输出,默认情况下,printf不输出换行printf "%-5s %-10s %-4s\n" No. NAME Markprintf "%-5s %-10s %-4s\n" 1 Lee 80printf "%-5s %-10s %-4.2f\n" 2 Woon 90.456printf "%-5s %-10s %-4.2f\n" 3 James 85.654321printf "%-5s %-10s %-5.3f\n" 4 Jeff 85.123789

Operation Result:

The shell`s name is shell_print.shHello World!Hello World!Hello WorldHello WorldHello   World!-e Hello    World!No.   NAME       Mark1     Lee        80  2     Woon       90.463     James      85.654     Jeff       85.124

2. Environment variables

#!/bin/bash#author by woon#env#获取全局环境变量env > /tmp/env_$(date +%Y%m%d).$$head -n 10 /tmp/env_$(date +%Y%m%d).$$echo "^^^^^^^^^^^^^^^^^分隔符^^^^^^^^^^^^^^^^^^^^^^" #获取单个全局变量的值echo $PATHecho "^^^^^^^^^^^^^^^^^分隔符^^^^^^^^^^^^^^^^^^^^^^"#自定义变量和自定义全局变量,删除环境变量var="Hello World"echo -n "$var" echo -n $varecho ${var}#设置全局环境变量export VAR#获取当前的shell版本echo $SHELL#orecho $0#获取变量的长度var="Hello World"echo ${#var}#变量内容的删除path=$PATHecho $pathecho "最短删除,非贪婪模式,由前向后删除"var1=${path#/*:}echo $var1echo "贪婪模式,删除匹配到最长的,由前向后删除"var2=${path##/*:}echo $var2echo "非贪婪模式%,由后向前删除"var3=${path%:*bin}echo $var3echo "贪婪模式,由后向前删除最长的"var4=${path%%:*bin}echo $var4

3. Shell Mathematical Operations

#!/bin/bash#author by woon.#数学运算sum_jia=0sum_cheng=1i=1while [ $i -le 10 ]; do     let "sum_jia+=i"     let "sum_cheng*=i"     let "i += 2"donesum_jian1=$[ sum_cheng - sum_jia]sum_jian2=$(( sum_cheng - sum_jia ))echo $sum_jiaecho $sum_chengecho $sum_jian1echo $sum_jian2

Run results
$./shell_num.sh
25
945
920
920

Linux Shell Example (i)

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.