Shift in shell programming

Source: Internet
Author: User

#! /Bin/sh
Files = ""
Trcase = ""
EXT = ""
Opt = No

# Gets called when a conversion fails
Error_msg ()
{
_ Filename = $1
Echo "'basename $ 0': Error The Conversion failed on $ _ filename"
}

If [$ #-EQ 0]
Then
Echo "for more info try 'basename $ 0' -- help"
Exit 1
Fi

While [$ #-GT 0]
Do
Case $1 in
-U) trcase = upper
EXT = ". UC"
Opt = Yes
Shift
;;
-L) trcase = lower
EXT = ". LC"
Opt = Yes
Shift
;;
-Help) echo "convert a file (Files) to uppercase from lowercase"
Echo "convert a file (Files) from lowercase to uppercase"
Echo "will convert all characters according to the specified command option ."
Echo "where option is"
Echo "-l convert to lowercase"
Echo "-u convert to uppercase"
Echo "The originak files is not touched, a new file will be created with either a. UC or. LC extension"
Echo "Usage: $0-[l] [u] File"
Exit 0
;;
-*) Echo "Usage: 'basename $ 0'-[l] [u] File"
Exit 1
;;

# Collect the files to process
*) If [-F $2]
Then
# Add the filenames to a variable list
Files = $ Files "" $1
Else
Echo "'basename $ 0': error can not find the file $1"
Fi
Shift
;;
Esac
Done

# No options given... help the user

If ["$ Opt" = "no"]
Then
Echo "'basename $ 0': error you need to specify an option no action taken"
Echo "Try 'basename $ 0' -- help"
Exit 1
Fi

# Now read in all files
For Loop in $ files
Do
Case $ trcase in
Lower) Cat $ loop | TR "[A-Z]" "[A-Z]"> $ loop $ ext
If [$? ! = 0]
Then
Error_msg $ Loop
Else
Echo "converted file call $ loop $ Ext"
Fi
;;
Upper) Cat $ loop | TR "[A-Z]" "[A-Z]"> $ loop $ ext
If [$? ! = 0]
Then
Error_msg $ Loop
Else
Echo "converted file call $ loop $ Ext"
Fi
;;
Esac
Done

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.