Shell Basics (ii)

Source: Internet
Author: User
Tags case statement echo command

echo command

The echo command for the Shell is the output for the string.

#!/bin/shread name #读取标准输入的行echo "$name It is a test" echo-e "yeah! \c "#-E Open escape \c No Line echo" It is a test "

printf command

printf is used as a formatted string, as well as the width of the string, left and right alignment, and so on. Default printf does not automatically add line breaks like echo, we can add \ n manually.

#!/bin/bashprintf "%-10s%-8s%-4s\n" name sex weight kg  printf "%-10s%-8s%-4.2f\n" Guo Jing male 66.1234 printf "%-10s%-8s%-4.2f\n "Yang over male 48.6543 printf"%-10s%-8s%-4.2f\n "Guo Fu 47.9876# One of the last parameters is" Def ", and%c automatically intercepts the first character of the string as the result output. $  printf "%d%s%c\n" 1 "abc" "DEF" 1 ABC D

  

%d%s%c%f format override:

d:decimal Decimal Integer --the corresponding positional parameter must be a decimal integer, otherwise error!

s:string String --the corresponding position parameter must be a string or character type, otherwise error!

C:char Character --the corresponding position parameter must be a string or character type, otherwise error!

F:float floating point --the corresponding position parameter must be a digital type, otherwise error!

Shell Test Command

The test command in the shell is used to check if a condition is true, and it can be tested in three aspects of numeric, character, and file.

 

Num1=100num2=100if Test $[num1]-eq $[num2]then    echo ' two numbers equal! ' Else    Echo ' two numbers are not equal! ' Fiif test-e./bashthen    echo ' file already exists! ' else    echo ' file does not exist! ' Fi

 Process Control

If you judge else no execution command is not written, you can write a line (separated by semicolons).

a=10b=20if [$a = = $b]then   echo "a equals B" elif [$a-gt $b]then   echo "A greater than B" elif [$a-lt $b]then   echo "A is less than B "Else   echo" does not meet the conditions "fi

For loop

When the value of the variable is in the list, the For loop executes all commands once, using the variable name to get the current value in the list. The command can be any valid shell command and statement. The in list can contain replacements, strings, and file names.
Can be written as a line for thevar in... itemn; Do Command1; command2. Done ;

While statement

The while loop is used to continuously execute a series of commands and to read data from the input file; the command is usually a test condition.

#!/bin/shint=1while ($int <=5)    do echo $int let    "int++" Done#let command is a tool for calculations in BASH that executes one or more expressions and does not need to be added in the calculation of variables $ to represent the variable. If a space or other special character is included in an expression, it must be caused.

Until cycle

The Until loop executes a series of commands until the condition is true.

The Until loop and the while loop are just the opposite of the processing mode.

The general while loop is better than the until loop, but at some point-and only in rare cases-the until loop is more useful.

Until syntax format:

Until conditiondo    commanddone

Case

The Shell Case statement is a multi-select statement. A case statement can be used to match a value with a pattern, and if the match succeeds, the matching command is executed.

The case works as shown above. The value must be followed by the word in, and each pattern must end with a closing parenthesis. The value can be either a variable or a constant. After the match discovery value conforms to a pattern, all commands begin to execute until;;.

The value will detect each pattern that matches. Once the pattern matches, the other mode is no longer resumed after the corresponding command is executed. If there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command.

#!/bin/bash# in the loop, sometimes you need to force out of the loop when the loop end condition is not reached, and the shell uses two commands to implement the function: Breakand continue. while:d o    echo-n "Enter a number from 1 to 5:"    read Anum case    $aNum in        1|2|3|4|5) echo "The number you entered is $aNum!"        ;        *) echo "The number you entered is not between 1 and 5!" Game Over "            break        ;;    Esacdone

  

  

Shell Basics (ii)

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.