Linux Experiment 4shell Programming (2)

Source: Internet
Author: User
Tags echo display

Shell Programming (2) 1. Basic usage of shell variables and usage of common symbols

(1) Change the theme specifier to the user's home directory name

?

(2) Assign the string DOS file c:> $student * to the variable x and display it

?

(3) In the shell command Terminal input likes= (Cosmos Galaxy Moon); Likes[6]=mars, and then uses echo to display the values of the following expressions, together with the result, to write out the function of the expression.

?

    • ${likes[*]}: Show all non-empty elements
    • ${like[@]}: Above, characters embedded in element values appear as field separators
    • ${#like [*]}: Displays the number of element values that have been set
    • ${#like [@]} function: Ibid.

      (4) In the shell command Terminal input Name=phoenix, and then use the Echo display expression ①, observe the results, and then enter the command unset name, and then enter the expression ① observation results. Combine two results to write the function of the expression.

    • Assign a value to a variable with the parameter substitution, and if the value of the variable is null, the result of the output is the given character, otherwise the string is assigned to the variable name

(5) Enter Name= '/usr/share/doc/apg/php.tar.gz ' at the shell command terminal, then use echo to display the values of the expression ① and ② respectively, and observe the results.

?

    • ${name%%.*}: Removes the value in the first format that matches the most part
    • ${NAME%.*}: If the end of the content is matched before and after, then the value of the end match is removed

      (6) Enter name=/usr/bin/x11 at the shell command Terminal, and then use echo to display the values of the expression ① and ② respectively, and observe the results. Modify the value of name, let Name= '/etc/apt/sources.list.d ', and again use echo to display the values of the expression ① and ② respectively, and observe the results. Combining the results, write out the function of the expression.

?

    • ${name#*/}: Remove the value that matches the beginning of the name value
    • ${name##*/}: Remove the matching part from the above format

(7) A student is known to submit a blog post page address as follows: address= ' http://www.cnblogs.com/xyz/p/8569001.html ' through string matching, how to get its blog home address: Homepage

   #!/bin/bash   address="‘http://www.cnblogs.com/xyz/p/8569001.html‘"   homepage="homepage=""${address%/p*}""‘"   echo $homepage

2. Shell Scripting Analysis

(1)

   #!/bin/bash   # p145 4.8      count=$#   cmd=echo   while [ $count -gt 0 ]   do           cmd="$cmd \$$count"           count=`expr $count - 1`   done   eval $cmd

    • Perform. ex1.sh, without parameters, not conforming to the while statement, so there is no output
    • Perform. Ex1.sh best wishes, there are two parameters, meet the conditions, and finally run the cmd command
    • Perform. Ex1.sh God bless u, the principle is ditto
    • function is flashback output
    • There are several parameters on the loop several times
    • Perform. Ex1.sh God bless u when the eighth line of CMD executes after echo $

(2)

   #!/bin/bash   # p145 4.10      IS_USER_FOUND=0   date +%F   if who | grep "^$1"; then           IS_USER_FOUND=1;           write $1 <<Limitstring           hello   Limitstring   fi      if [ $IS_USER_FOUND -eq 0 ]; then           echo "user $1 is not found."   fi

    • Line 6th to determine if the user name is correct
    • 第8-10, tell the first parameter redirect, send Hello to it.

(3)

   #!/bin/bash      suffix=BACKUP--`date +%Y%m%d-%H%M`      for script in *.sh; do     newname="$script.$suffix"     echo "Copying $script to $newname..."     cp $script $newname   done

    • Copy the SH format file in the current folder

(4)

   !/bin/bash   # realize function same as Command cat -n      function NL() {           while read x           do                   (( ++line ))                   echo "$line $x"           done   }         line=0   if [ $# -eq 0 ]; then           NL   else           while [ "$1" ] && [ -f "$1" ]           do                   NL < $1                   shift           done   fi

    • The function of the 4-10 row is to save the user input value in the variable x, and then output the line number and the value of X
    • 14 Line, execute
    • 17-21, accept user input, move parameter left one
3. Shell Script Writing

(1)

   #!/bin/bash   dir=$1   shift   for script in [email protected]   do   cp $1 $dir   done

(2)

   #!/bin/bash   cd $1   shift   while [ $1 ]   do   file=$1   cat $1   shift   done

(3)

   #!/bin/bash   for script in *.c   do   mv $script $x   done   cd $x   ls -Sl

(4)

   #!/bin/bash   read str   echo $str | cut -c $1-$2

(5)

   #!/bin/bash   echo "Please input the num(1-100) "         read num   radent=$((RANDOM%))          while [[ "$num" != $radent ]]         do          if [ "$num" -lt $radent ]          then               echo "small"                 read num          elif [ "$num" -gt $radent ]          then                echo "high"                   read num           fi       done          echo "Yes! "

Linux Experiment 4shell Programming (2)

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.