Bash Shell Scripting Learning (ii)

Source: Internet
Author: User

Write a second shell script today and write the after-class questions in "private cuisine". The problem needs to write a check whether a name exists, does not exist to create a file, there is a need to determine whether the name of the file, the file is deleted after the creation of a directory with the same name, if the directory is to delete this directory.

First set up a basic condition: The Operation directory is/tmp/test/(note: If this directory does not exist you need to create manually), you need to establish a file or directory name of logical.

Start writing scripts below.

#!/bin/bash#program:#    this program to check if a filename  exists&nbsp #History       #2016/04/08    mg     First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/sbin:~/binexport  pathecho -e  "Checking if/tmp/test/logical is present, please later ..." test -e  "/tmp/test/logical"  &&  state=0 | |  state=1path= "/tmp/test" filename= "logical" case  $state  in     "1")          echo  "The file does not exist, is creating ..."          mkdir -p  $path  && touch  "$path/$filename"          echo  "File created successfully! "     ;;      *)          test -d  "$path /$filename " && STATU=0&NBsp;| | statu=1             if [   "$statu"  ==  "0"  ];then                  echo  $filename   exists and is a directory. "                 rm  -rf  "$path/$filename"                   echo  "deleted this directory! "              else                   echo  "$filename   exists and is a file, deleting and creating a directory with the same name "                                      rm -f  "$path/$filename"  && mkdir  "$path/$filename"                    echo  "Creating now Please ..."                    echo  "$filename   Catalogue has been established! "              fi      ;;  esac

For simple instructions, write the logic of the script. Use the TEST-E command to determine if the "logical" name exists, if no status is set to "1" and create a file based on the topic. If there is a test-d command to determine whether the directory, if the directory is set its state to 0, and the directory through the RM-RF command to delete (note: If you do not add the-R parameter can not delete the directory, so you need to join the-R parameter), if the file is the rm-f command to delete the file, Create a directory with the same name by using the mkdir command (Note: "Command 1 && command 2" means that if command 1 executes successfully, the command 2 continues to execute).

The script function is relatively simple, there will be some shortcomings, continue to work to improve it.

This article from "Product small stream, into Jianghai" blog, please be sure to keep this source http://goodjoe.blog.51cto.com/9173556/1761796

Bash Shell Scripting Learning (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.