Shell Programming Basics

Source: Internet
Author: User

For C in a B C D E F g;do echo $c; Done sequentially output


For C in a B C D E F g;do tar czvf install.log. $c. tar.gz Install.log; Done

For C in {a.. g}; do echo $c; Done the same as above


Seq 1 10 columns of numbers

For i in {1..10}; do echo $i; Done

String variable instance
Str= ' I Love Linux. I love Unix too. '

Echo ${str} calculates string length

echo ${str:5} intercepts str from the fifth character to the end of the string

Echo ${str:7:5} intercepts the fifth character of Str starting with the seventh character


echo ${str#i Love} Delete the starting string

echo ${str#i*. Delete all characters starting from I to., Shortest match

echo ${str# #I *} Delete all characters from start I to., Longest match

Test is the meaning of testing

Shell Programming:
Single Branch
#!/bin/bash
Filename=/myshell/test1
If Test-f $filename; Then
Echo ' The file is exist. '
Fi


Dual Branch
#!/bin/bash
Filename=/myshell/test1
If Test-f $filename; Then
Echo ' The file is exist. '
Else
Echo ' The file is not exist. '
Fi

#!/bin/bash
Filename=/myshell/test2
If [-f $filename]; Then
Echo ' The file is exist. '
Else
Echo ' The file is not exist. '
Fi


Judge
#!/bin/bash
A=this
B=there
[$a! = $b]&&echo Yes | | Echo No

#!/bin/bash
Filename=/myshell/test3
Test-f $filename && echo ' exist ' | | echo ' not exist '
test-d $filename && echo ' exist ' | | echo ' not exist '
Test-r $filename && echo ' exist ' | | echo ' not exist '
Test-w $filename && echo ' exist ' | | echo ' not exist '
Test-x $filename && echo ' exist ' | | echo ' not exist '

Multi-Branch
#!/bin/bash
Echo ' please input your number. '
Read number
if [$number = = 1]; Then
echo ' Your input number is 1. '
elif [$number = = 2]; Then
echo ' Your input number is 2. '
elif [$number = = 3]; Then
echo ' Your input number is 3. '
Else
echo ' I dont know what do you input. '
Fi

Echo ' please input your hardware. '
Read HD
if [$HD = = CPU]; Then
Echo ' Your CPU info is. '
Cat/proc/cpuinfo
elif [$HD = = mem]; Then
Echo ' Your mem info is. '
Cat/proc/meminfo
elif [$HD = = hard]; Then
Echo ' Your harddisk info is. '
Df-h
Else
echo ' I dont know what do you input. '
Fi

Echo ' please input your number. '
Read number
Case $number in
1)
echo ' Your input number is 1. ';;
2)
echo ' Your input number is 2. ';;
3)
echo ' Your input number is 3. ';;
*)
echo ' I dont know what do you input. ';;
Esac

echo ' please input kemu. '
Read Kemu
Case $kemu in
Shuxue)
Echo ' Your input is shuxue. ';
中文版)
Echo ' Your input is 中文版 ';;
Yuwen)
Echo ' Your input is Yuwen. ';
*)
echo ' I dont know what do you input. ';;
Esac


While loop:

#!/bin/bash
# []-eq-ne-gt-ge-lt-le
# (()) = = = = > >= < <=
#program Summary

i=10
#while (($i >5));d o
While [$i-gt 5];d o
echo $i;
#if [$i-eq 8]; then
# echo ' The number is 8. '
#fi
((i--));
Done


Until cycle:

#!/bin/bash

a=10
Until (($a <0));d o
echo $a;
((a--));
Done


For loop:
#!/bin/bash
For ((i=1;i<=10;i++));d o
echo $i;
Done


#!/bin/bash
Echo ' please input an numer. '
Read number
For ((i=1;i<= $number; i++));d o
echo $i;
Done


Function:

#!/bin/bash
function print () {
echo "Your input is $x"
}

echo "This program would print you selection!"

Case $x in
"One")
print 1;;
"Both")
Print 2;;
"Three")
Print 3;;
*)
Print "Usage $ one|two|three";;
Esac

Shell Programming 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.