当前位置:网站首页>Shell learning

Shell learning

2022-06-22 16:28:00 The force is with you

Output hello world

//  Designated for /bin/bash The interpreter under 
#!/bin/bash
echo "Hello World !"

function shell There are two ways

1、 As an executable 
chmod +x ./test.sh  # Give the script permission to execute 
./test.sh  # Execute the script 

2、 As interpreter parameters 
//  choose /bin/sh Interpreter execution under  test.sh file 
/bin/sh test.sh

shell The variable of

 assignment :
name="test name"
 Be careful name and = There must be no space between them 

 You can use statements to assign values to variables :
for file in `ls /etc`  or  for file in $(ls /etc)

 Using variables :
${name}

 A read-only variable :
readonly name

 Delete variables :
unset name

 Variable type :
	 local variable 
	 environment variable 
	shell Variable 

shell String

 String assignment :
str='str' //  Single quotation marks 
str="str" //  Double quotes 
str=str //  No quotes 

 Get string length :
string="abcd"
echo ${
    #string} # Output  4

 Extract substring :
string="runoob is a great site"
echo ${string:1:4} #  Output  unoo

 Find substrings 
string="runoob is a great site"
echo `expr index "$string" io`  #  Output  4

shell Pass parameters

echo "Shell  Pass parameter instance !";
echo " File name of execution :$0";
echo " The first parameter is zero :$1";
echo " The second parameter is :$2";
echo " The third parameter is zero :$3";

 Output 
$ chmod +x test.sh 
$ ./test.sh 1 2 3
Shell  Pass parameter instance !
 File name of execution :./test.sh
 The first parameter is zero :1
 The second parameter is :2
 The third parameter is zero :3

shell Array

array_name=(value1 value2 ... valuen)

array_name[0]=value0
array_name[1]=value1
array_name[2]=value2

${array_name[index]}

echo " The elements of the array are : ${my_array[*]}"
echo " The elements of the array are : ${my_array[@]}"

shell Operator

 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[The force is with you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221508060655.html