#!/bin/sh### globalsimg_ext= "{png,jpg,gif}" sql_file= "My_images_mysql.sql" sql_ins= "INSERT INTO images values ("Sql_imageid_range=0sql_imagetype=1sql_name=" "sql_image=" "### ERRORNORMAL=0ERR_ Args=1err_no_dir=2err_no_file=3retval= $NORMAL ################################################################# ########## actual main####################################################################### #main () { local dir=$1 local num=$2 # check the number of command argument if [ $# -lt 2 ]; then return $ERR _args fi # check target dir [ ! -d $dir ] && return $ERR _no_dir # check target file check_image_file $dir || return $? # make sql file make_sql_ file $num return $RETVAL}#################################################### ####################### check image files existence####################################### ################################ #check_image_file () { local dir=$1 local file_num=0 file_num=$ (eval ls $dir/*. $IMG _ext 2>/dev/null | wc -l) [ $file _num -eq 0 ] && return $ERR _no_file return $RETVAL}#################### ####################################################### make sql file to insert image files####################################################################### #make_sql_file () { local f= [ -f $SQL _file ] & & rm -f $SQL _file sql_imageid=$1 for f in $ (eval ls $dir/*. $IMG _ext 2>/dev/null) do sql_name=$ (basename $f | cut -d. &NBSP;-F1) sql_image= "0x$ (od -tx1 $f | awk ' {for (i=2; i<=nf; i++) printf ('%s ', toupper ($i)} ') ' echo "$SQL _ins $SQL _imageid, $SQL _imagetype, ' $SQL _name ', $ Sql_image); " >> $SQL _file sql_imageid=$ ($SQL _imageid + 1)) &NBsp; echo -n "." # in progress done echo -e "\ ncompleted " return $RETVAL}####################################################### #################### check error and display error message###################### ################################################# #check_error () { local result=$1 case $result in $ERR _args) usage ;; $ERR _no_dir) echo "Cannot find target dir" ;; $ERR _no_file) echo "cannot find \" *. $IMG _ext\ " files" ;; *) echo "Unknown error" ;; esac return $result}###################################### ##################################### usage############################################################## ######### #usage () { echo "usage: make_img_insert_sql.sh <dir> <start_imageid> "}########################################################################### script main####################################################################### #main "[email protected]" | | check_error $?
1. Various $ variables:
The execution name of this program $n the nth parameter value of the program, n=1..9$* all parameters of the program, which can be more than 9 parameters. $# the number of parameters for this program $$ the PID of the program (the current process ID number of the script run) $! Perform the PID of the previous background instruction (process ID number of the last process running in the background) $? Executes the return value of the previous instruction (displays the exit status of the last command. 0 means no error, any other value indicates an error) $-shows the current options used by the shell, same as the set command [email protected] similar to $*, but can be used as an array
2. Eval usage: Theeval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for a time that does not function. This command scans the variable two times. These variables, which need to be scanned two times, are sometimes referred to as complex variables. But the variables themselves are not complicated. The eval command can also be used to echo simple variables, not necessarily complex variables.
The 1 eval command can also be used to echo simple variables, not necessarily complex variables
For example:
[Email protected] ~]$ Name=valuebank [[email protected] ~]$ eval echo $NAME valuebank [[email protected] ~]$ echo $NAM E Valuebank
2 executing a command containing a string
First we first create a small file named Test that contains some text in this small file. Next, assign the cat test to the variable myfile, and now we e cho the variable to see if we can execute the above command.
[Email protected] ~]$ VI test [[email protected] ~]$ CAT test Hello World!!! I am a Chinese boy!
Assigns the cat testf to the variable myfile
[Email protected] ~]$ myfile= "Cat test"
If we echo the variable, we will not be able to list the contents of the test file.
[Email protected] ~]$ echo $myfile cat test
Let's try the eval command and remember that the eval command will scan the variable two times.
[Email protected] ~]$ eval $myfile Hello World!!! I am a Chinese boy!
As you can see from the above results, you can use the Eval command not only to displace the variable, but also to execute the corresponding command. The first scan has a variable substitution,
That is, eval cat test, the second scan executes the command contained in the string, cat test.
The 3 command can also be used to display the last parameter passed to the script
[email protected] ~]$ cat test1 #!/bin/bash echo "Total of the arguments passed $#" echo "The process Id is $$" echo "Last Argument OS" $ (eval echo \$$#) "[[E-mail protected] ~]$./test1 Value Bank test last total of the arguments passed 4 The process Id is 21545 last argument os last
In the above script, the backslash represents the escape character's consciousness, so the first $ in the first substitution is only as normal ' $ ', the eval command first puts $ #解析为当前shell的参数个数 "$ (eval echo $4)", and then the eval will execute the echo command on the second scan, and the outer layer "" has the parsing function, will replace the $4 with the value (the 4th parameter), so the end is to execute echo last, to arrive at the final parameter.
4 give each value a variable name
You can give a value a variable name. Let me explain this, assuming there is a file named Test2,
You want the first column in the file to be the variable name, and the second column to be the value of the variable, so you can:
[email protected] ~]$ cat test2 commany TQ LANGUE 中文版 like YES [[email protected] ~]$ cat test3 #!/bin/bash WHI Le read NAME VALUE do eval "${name}=${value}" did echo "$COMMANY $LANGUE $LIKE" [[email protected] ~]$./TEST3 TQ E Nglish YES
Add:
Eval: In fact, the role of command parsing, "${name}=${value}" in the ' = ' is a copy process.
READ: Reads a row of parameters from the categorization malleability input, and then splits the parameters into parameters by delimiter. The read here is not stdin two is the Test2 file because there is a redirect done
This article is from the creator think blog, so be sure to keep this source http://strongit.blog.51cto.com/10020534/1710379
Shell Script Case Analysis