Linux shell script is a comprehensive study 6

2010-12-07  来源:本站原创  分类:OS  人气:94 

# Input check:
if [-z "$ 1"]; then
error "ERROR: you must specify a file, use-h for help"
fi
filen = "$ 1"
# Rename any .1, .2 etc file:
for n in 9 8 7 6 5 4 3 2 1; do
if [-f "$ filen. $ n"]; then
p = `expr $ n + 1`
echo "mv $ filen. $ n $ filen. $ p"
mv $ filen. $ n $ filen. $ p
fi
done
# Rename the original file:
if [-f "$ filen"]; then
echo "mv $ filen $ filen.1"
mv $ filen $ filen.1
fi
echo touch $ filen
touch $ filen
How does this script work? In testing the user provides a file name later, we were a 9-1 cycle. 9, 10, was named the file, rename the file 8, 9 and so on. Cycle is complete, we will file an original file name and also create a blank file the same name as the original file.
Debug command to debug the most simple of course, is to use the echo command. You can use the echo in the place of any suspected errors to print any variable value. This is also the vast majority of shell programmers to spend 80% of the time to debug the program causes. Shell program has the advantage of not re-compile, insert a echo command does not need much time.
shell also has a real debug mode. If the script "strangescript" in error, you can do to debug:
sh-x strangescript
This will execute the script and display the values of all variables.
There is also a shell script that just do not need to check the syntax of the implementation of the model. You could use:
sh-n your_script
This will return all syntax errors.

相关文章
  • Linux shell script is a comprehensive study 2010-11-04

    Transfer from Source: Linux Commune of: Linux http://www.linuxidc.com/Linux/2007-06/4767.htm 1. Linux based scripting 1.1 Syntax Basics 1.1.1 at the beginning Program must begin with the following line (be the first line in the file side): #! / Bin /

  • Linux shell script is a comprehensive study 4 2010-12-07

    5. Quotation marks to pass any parameters to the program before the program will expand wildcards and variables. This means that the so-called program will expand the wildcard (eg *) replace the appropriate file name, it is replaced by the variable v

  • Linux shell script is a comprehensive study 6 2010-12-07

    # Input check: if [-z "$ 1"]; then error "ERROR: you must specify a file, use-h for help" fi filen = "$ 1" # Rename any .1, .2 etc file: for n in 9 8 7 6 5 4 3 2 1; do if [-f "$ filen. $ n"]; then p = `expr $ n + 1`

  • Linux shell script is a comprehensive study of 4 2010-12-07

    5 quotes in passing any parameters to the program before the program will expand wildcards and variables. This so-called extension means that the program will wildcards (like *) with the appropriate file name, which replaced the variable variable val

  • Linux shell script is a comprehensive study of 6 2010-12-07

    # Input check: if [-z "$ 1"]; then error "ERROR: you must specify a file, use-h for help" fi filen = "$ 1" # Rename any .1, .2 etc file: for n in 9 8 7 6 5 4 3 2 1; do if [-f "$ filen. $ n"]; then p = `expr $ n + 1`

  • Linux shell script is a comprehensive study 2 2010-12-07

    2) The concept: pipes, redirection and backtick These are not system commands, but they are really important. Pipe (|) the output of one command as input to another command. grep "hello" file.txt | wc-l Search in file.txt contains "hello&qu

  • Linux shell script is a comprehensive study 3 2010-12-07

    2.case case: the expression can be used to match a given string, not numbers. case ... in ...) Do something here;; esac Let's look at an example. file command can identify a given file type, such as: file lf.gz This returns: lf.gz: gzip compressed da

  • Linux shell script is a comprehensive study of 2 2010-12-07

    2) The concept: pipes, redirection and backtick These are not system commands, but they are really important. Pipe (|) the output of one command as input to another command. grep "hello" file.txt | wc-l Search in file.txt contains "hello&qu

  • Linux shell script is a comprehensive study of 3 2010-12-07

    2.case case: the expression can be used to match a given string, not a number. case ... in ...) Do something here;; esac Let's look at an example. The file command can identify a given file type, such as: file lf.gz This will return: lf.gz: gzip comp

  • Linux shell script is a comprehensive study 5 2010-12-07

    Let us look at two examples: Binary to decimal conversion script b2d the binary number (eg 1101) into the corresponding decimal number. It is also a mathematical operation with the expr command examples: #! / Bin / sh # Vim: set sw = 4 ts = 4 et: hel

  • Linux shell script is a comprehensive study of 5 2010-12-07

    Let's look at two examples: Binary to decimal conversion script b2d a binary number (eg 1101) into the corresponding decimal number. This is a mathematical operation with the expr command examples: #! / Bin / sh # Vim: set sw = 4 ts = 4 et: help () {

  • Linux shell script based learning (8) 2010-04-17

    Source: http://os.51cto.com/art/201003/186835.htm Linux shell script is to specify the instance in front of the decimal and binary conversions, but also an instance of the file loop to end this part of the study. I believe Linux shell script based le

  • Linux shell script based learning (1) 2010-04-17

    Source: http://os.51cto.com/art/201003/186779.htm Linux shell script based learning where we first come first lecture introduces the syntax of shell foundation, at the beginning, comments, variables and environmental variables, to tell you to do a ba

  • Linux shell script based learning (2) 2010-04-17

    Linux shell script based courses were introduced in front of a talk at the beginning of the syntax-based, comments, variables and environmental variables, there will introduce the shell command and control processes of the first part, in the shell sc

  • Linux shell script based learning (3) 2010-04-17

    Source: http://os.51cto.com/art/201003/186820.htm Linux shell script based learning about the third, in front of us shell commands and process control, the lack of space did not speak process control, process control today introduced here is only the

  • Linux shell script based learning (4) 2010-04-17

    Source: http://os.51cto.com/art/201003/186826.htm On a Linux shell script based learning, we talked about Linux shell script to control the process if, select, case, where then introduced the Linux shell script control flow of the loop and the quotat

  • Linux shell script based learning (5) 2010-04-17

    Source: http://os.51cto.com/art/201003/186827.htm Linux shell script base has been divided into several parts, and where the contents of the control process also once finished, this is the last part on the here document, here a little bit complicated

  • Linux shell script based learning (7) 2010-04-17

    Source: http://os.51cto.com/art/201003/186831.htm Linux shell script based learning in front of this part if only look at the theory among some although there are examples, but not enough systems for everyone here will show specific examples of Linux

  • A comprehensive study of a Linux shell script 2010-12-07

    1. Linux 1.1-based syntax of scripting a basic introduction to 1.1.1 at the beginning of the program must start with the following line (be the first line in the file side): #! / Bin / sh Symbol #! Used to tell the system behind the argument that it

  • Comprehensive learning a Linux shell script 2010-12-07

    1. Linux 1.1 syntax based scripting a basic introduction to 1.1.1 at the beginning of the line program must begin with the following (must be square in the first line of file): #! / Bin / sh Symbol #! Behind it to tell the system parameters are used