Linux shell-Common scripts

Source: Internet
Author: User
Tags readable

1. Determine the logged-on user

1.1 Scripts

[Email protected]_1 shell]$ VI check_user.sh#! /bin/shecho "You is logged in as ' WhoAmI '"; if [' whoami '! = Devtac]; Then  echo "must is logged in as Devtac-to-run this script."  Exitfiecho "Running script at ' Date '"

1.2 Running results

[Email protected]_1 shell]$ chmod a+x check_user.sh [[Email protected]_1 shell]$./check_user.sh you is logged in as Dev tacrunning script at December 09, 2014 Tuesday 13:35:17 CST

2. Determine whether to continue execution

2.1 Scripts

[Email protected]_1 shell]$ VI do_continue.sh#! /bin/shdocontinue=necho really want to continue? (y/n) "Read Docontinueif [" $doContinue "! = y]; Then   echo "quitting ..."   Exitfiecho "OK ... we'll continue."

2.2 Running Results

3 Hidden input

3.1 Scripts

[Email protected]_1 shell]$ VI hide_input.sh#! /bin/shstty-echoecho-n "Enter The database system password:" Read Pwstty Echoecho "$PW was entered"

3.2 Results

3.3 Parsing

Stty command

3.3.1 Man Manual definition

DESCRIPTION       Print or change terminal characteristics.

[Email protected]_1 shell]$ stty-aspeed 38400 baud; Rows 47; Columns 125; line = 0;intr = ^c; Quit = ^\; erase = ^?; Kill = ^u; EOF = ^d; EOL = <undef>; Eol2 = <undef>; Swtch = <undef>; start = ^q; stop = ^s;susp = ^z; Rprnt = ^r; Werase = ^w; Lnext = ^v; flush = ^o; min = 1; Time = 0;-parenb-parodd CS8-HUPCL-CSTOPB Cread-clocal-crtscts-cdtrdsr-ignbrk-brkint-ignpar-parmrk-inpck-istrip -INLCR-IGNCR icrnl ixon-ixoff-iuclc-ixany-imaxbel-iutf8opost-olcuc-ocrnl Onlcr-onocr-onlret-ofill-ofdel nl0 cr 0 tab0 bs0 vt0 ff0isig icanon iexten echo echoe echok-echonl-noflsh-xcase-tostop-echoprt echoctl Echoke

The parameters used in this example

       [-]echo              echo Input characters

Shielding display
Stty-echo #禁止回显
Stty Echo #打开回显
Test method:
Stty-echo;read;stty Echo;read

Summary: The effect of using Stty-echo is like when we enter a Linux login password, we don't see the input

4 Deciding whether to catalog

4.1 Scripts

[Email protected]_1 shell]$ VI is_a_directory.sh #! /bin/shif [-Z "$"]; Then echo "echo" error:invalid number of   arguments "   echo" Usage: $ arg1 "   echo" "   Exitfiif [-D $ 1]; Then   Echo, "is a directory." Else   echo "is not a directory." Fi

4.2 Test Results

[Email protected]_1 shell]$/is_a_directory.sh error:invalid number of argumentsusage:./is_a_directory.sh Arg1[[emai L protected]_1 shell]$./is_a_directory.sh $PWD/home/devtac/shell is a directory.

4.3 parsing

4.3.1 Script Pass (not all tested, for reference only)

$ A script name
Location parameter #1
$-$9 positional parameter #2-#9
${10} positional Parameters #10
$# Number of positional parameters
"$*" all positional parameters (as a single string) *
"[Email protected]" All positional parameters (each as a separate string)
${#*} The number of command-line arguments passed to the script
${#@} The number of command-line arguments passed to the script
$? return value
Process ID (PID) of the $$ script
$-the flags passed to the script (using set)
$_ the last parameter of the previous command
$! Process ID (PID) of the last job running in the background

4.3.2 to determine if the parameter is empty, to interpret whether the directory exists

-Z String
True if the length of string is zero.

-D Directory

True if the directory exists.

5. Interpretation of the document is readable

5.1 Scripts

[Email protected]_1 shell]$ VI is_readable.sh#! /bin/shif [-Z "$"]; Then echo "echo" error:invalid number of   arguments "   echo" Usage: $ AGR1 "   echo" "   exitfiif [! -R $]; Then   echo "are not readable." Else   echo "is readable." fi~

5.2 Test Results

[Email protected]_1 shell]$/is_readable.sh error:invalid number of argumentsusage:./is_readable.sh agr1[[email Prot Ected]_1 shell]$./is_readable.sh Asdfasasdfas is not readable. [Email protected]_1 shell]$/is_readable.sh $PWD/home/devtac/shell is readable. [Email protected]_1 shell]$/is_readable.sh/home/devtac//home/devtac/is readable. [Email protected]_1 shell]$./is_readable.sh/home/devtac/shell/is_readable.sh/home/devtac/shell/is_readable.sh is Readable.

6 Output Script Parameters

6.1 Scripts

[Email protected]_1 shell]$ VI print_args.sh#! /bin/shwhile [$#-ne 0]   do      echo $      shift   Done

6.2 Output Results

6.3 Parsing

6.3.1 Shift Command

For positional variables or command-line arguments, the number must be deterministic, or when the shell program does not know its number, you can assign all parameters together to the variable $*. If the user requires that the shell do not know the number of positional variables, but also one by one to the parameter one by one processing, that is, after $ $, after the $ $ $, and so on. The value of the variable before the shift command executes is not available after the shift command executes.

7 Copy directory files to a directory

7.1 Scripts

[Email protected]_1 shelltemp2]$ VI copy_special_dir_file.sh


#! /bin/sh

#echo $#
# If the number of the args is not equal 2, output command usage and exit
If [$#-ne 2];then
echo "Usage: $ fromdir Todir"
Exit 1
Fi

Fromdir=$1 #from Directory
Todir=$2 #to Directory
#echo $fromdir $todir

#if Fromdir or Todir is not a valid directory, exit
if [!-D $fromdir] | | [!-D $todir];then
echo $fromdir or $todir is not a valid directory
Exit 1
Fi

For i in $fromdir/*; Do
If [-f $i]; Then
filename=${i# $fromdir/}
echo copying $i to $todir/$filename
Cp-p $i $todir/$filename
Fi
Done
Exit 0

7.2 Test Results

[[Email protected]_1 shell]$./copy_special_dir_file.sh/home/devtac/shell/home/devtac/shelltemp2/qewqe/home /devtac/shell OR/HOME/DEVTAC/SHELLTEMP2/QEWQE is not a valid Directory[[email protected]_1 shell]$ rm. /shelltemp2/*[[email protected]_1 shell]$./copy_special_dir_file.sh/home/devtac/shell/home/devtac/ Shelltemp2copying/home/devtac/shell/a.txt To/home/devtac/shelltemp2/a.txtcopying/home/devtac/shell/b.txt To/home /devtac/shelltemp2/b.txtcopying/home/devtac/shell/check_user.sh to/home/devtac/shelltemp2/check_user.shcopying/ Home/devtac/shell/copy_special_dir_file.sh to/home/devtac/shelltemp2/copy_special_dir_file.shcopying/home/ DEVTAC/SHELL/COPY_SPECIAL_DIR_FILE.SH.BK to/home/devtac/shelltemp2/copy_special_dir_file.sh.bkcopying/home/ Devtac/shell/do_continue.sh to/home/devtac/shelltemp2/do_continue.shcopying/home/devtac/shell/hide_input.sh to/ Home/devtac/shelltemp2/hide_input.shcopying/home/devtac/shell/is_a_directory.sh to/home/devtac/shelltemp2/iS_a_directory.shcopying/home/devtac/shell/is_readable.sh to/home/devtac/shelltemp2/is_readable.shcopying/home/ devtac/shell/print_args.sh to/home/devtac/shelltemp2/print_args.shcopying/home/devtac/shell/sh01.sh to/home/  Devtac/shelltemp2/sh01.sh[[email protected]_1 shell]$

7.3 Parsing

7.3.1filename=${i# $fromdir/}

Hypothesis: I=/home/devtac/shell/a.txt

Fromdir=/home/devtac/shell/a.txt

Then: Filename=a.txt

Reference: Source http://linux.chinaunix.net/techdoc/develop/2007/05/05/956956.shtml

For the sake of completeness, I'll use some examples here to illustrate some of the supernatural powers of ${}:
Suppose we define a variable as:
File=/dir1/dir2/dir3/my.file.txt
We can replace each other with ${} to get different values:
${file#*/}: Take out the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Take out the last/and left string: my.file.txt
${file#*.} : Take out the first one. And the string to the left: file.txt
${file##*.} : Take out the last one. And the string to the left: txt
${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3
${file%%/*}: Remove the first/its right string: (null value)
${FILE%.*}: Take off the last one. And the string to the right:/dir1/dir2/dir3/my.file
${FILE%%.*}: Take out the first one. And the string to the right:/dir1/dir2/dir3/my
The methods of memory are:

        # is to remove the left side (on the plate # on the left of the $)

        % is removed to the right (on the plate% on the right of the $)

    The
      single symbol is the minimum match, and the two symbol is the maximum match.


${file:0:5}: Extract the leftmost 5 bytes:/dir1
${file:5:5}: Extracts the 5th byte to the right of 5 consecutive bytes:/DIR2
We can also replace the string in the value of the variable:
${file/dir/path}: Change the first dir to Path:/path1/dir2/dir3/my.file.txt
${file//dir/path}: Change all dir to path:/path1/path2/path3/my

Network reference:

Http://www.ha97.com/4020.html

http://blog.csdn.net/tianlesoftware/article/details/5381984

http://blog.csdn.net/qzwujiaying/article/details/6371246

Http://linux.chinaunix.net/techdoc/develop/2007/05/05/956956.shtml

Books:

Http://www.comptechdoc.org/os/linux/usersguide/linux_ugshellpro.html

Linux shell-common scripts

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.