First record
1, the simple HelloWorld writing
Input command under Shell input:VI helloworld.sh
Then enter the text editor:
#!/bin/shell#this is Ahelloworld testa= "HelloWorld" echo $a
Execute helloworld.sh File
Command:
# sh helloworld.sh
2, the variable assignment method is
# variable_name = Variable_value
If you assign a value to a variable that already has a value, the new value supersedes the old value. To take a value in front of the variable name plus $, $variable _name can be used in quotation marks, if there is confusion, you can use curly braces to distinguish, for example:
# echo "Hi, $as"
Will not output "Hi, helloworlds", but output "Hi,". This is because the shell treats $as as a variable, and $as is not assigned a value, and its value is empty. The correct method is:
$ echo "Hi, ${a}s"
Variables in single quotes do not perform variable substitution operations.
3.who command to see who has logged in to the system
who| Wc–l Computing total number of users
4.cat > Nusers Creating File Nusers, using cat copy terminal input
echo "haha"
Then ctrl+d represents the end of the file
Use chmod +x nesers to let the file have permission to execute
Test:./nusers
Shell recognizes three types of commands: Built-in commands, shell functions, and external commands
The difference between 5.printf and Echo
printf does not have the ability to wrap lines like echo
6. Redirection and piping
7.shell command
# who see who's logged in # Date view current date # chmod +x file to modify file to make it executable # echo Display text string Contents # Cat file shows the contents of the file, and more almost #./file Execute File # s ET shows the complete environment variable configuration list # testing= ' Date ' action: output value; The date value is output and assigned to testing# $testing $ for the value of the output variable # command > OutputFile writes the results of command commands to the outputfile file (overwrite write) # command >> OutputFile writes the results of command commands to the OutputFile file (append write) # The WC << EOF prompt always prompts for input data, knows the input EOF, and then the WC command begins the inline input redirection provided by the Data execution line, Word, and byte count. # RPM–QA > Rpm.list to enter the installed package list data into rpm.list# sort rpm.list sorting rpm.list# Rpm–qa | Sort by pipe | Add two commands to a # expr 1 + 5expr to perform the addition operation, resulting in a floating-point arithmetic command # $6#BC? View exit status code for the last command
Second note
1.for command
Format:
for Var in listdocommandsdone
Write a program that iterates through the list, as follows:
[[Email protected] test]# VI sh1201#!/bin/shfor i in liudiwei haha xiaoli liangliang xuxudo echo ' This is my friend $i "Done
Test
[Email protected] test]#./sh1201this is my friend liudiweithis are my friend Hahathis is my friend Xiaolithis is my Frien D liangliangthis is my friend Xuxu
If the list contains single quotes, such as I ' m a student.
There are two ways to fix it:
(1) using the escape character I\ ' m a student
(2) Use double quotation marks to define the value used in single quotation marks. "I ' m" a student.
2.while command
The format of the while command:
While Test Commanddo other Commandsdone
Writing Testwhile
[Email protected] test]# VI testwhile#!/bin/shi=0while [$i-le]do echo "i= $i" i=$[$i + 1]done
Test:
[Email protected] test]#./testwhilei=0i=1i=2i=3i=4i=5i=6i=7i=8i=9i=10
3.util command
Command format
Until Test commandsdo other Commandsdone
Note: Until commands is true, the other commands is always executed.
4. Nested loop output 99 multiplication table
Edit a new file
[Email protected] test]# VI mutil99
99 Multiplication Table Code:
#/bin/shfor ((i=1;i<=9;i++)) do for ((j=1;j<=i;j++)) do echo-n "$j * $i =$[$i * $j]" done Echodone
Results:
[Email protected] test]#/mutil991*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5* 5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8= 40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
5.break and Continue
6. Processing the loop output
Iterate through the folder and determine whether it is a directory or a file
[Email protected] test]# vi isdirorfile#/bin/shfor file In/root/*do if [-D "$file] then echo" $file is a directory! " else echo "$file is a file!" Fidone > Output.txt #讲结果输出到output. txt file
Results
[Email protected] test]# More output.txt/root/anaconda-ks.cfg are a file!/root/dead.letter is a file!/root/downloads is a Directory!/root/hadoop is a directory!/root/hello.sh is a file!/root/initial-setup-ks.cfg are a file!/root/src is a directo Ry!/root/test is a DIRECTORY!/ROOT/TESTQ is a file!
Linux Series-shell Learning notes