Most of the commands in Linux do only one thing, so complex tasks can be accomplished by gluing various programs together using the Shell scripting language. This paper makes full use of powerful, but simple and easy to learn gadgets, to give some practical solutions to the problem, I hope to make Linux more practical and better use.
Let the command "speak"
Almost every Linux command has many options that allow users to tailor flexibly to their needs. But this brings a lot of inconvenience to beginners, one is to learn trouble, the second is easy to forget, need to come back again.
So is there any way to make the order to "talk"? The Shell scripting language allows you to add friendly "faces" to commands. Examples are as follows:
::::::::::::::
cname.sh
::::::::::::::
echo "Please input file name:"
read old
echo "Please input new name: "
read new
mv $old $new
In the code above, the MV command format is:
MV Original name New name
The echo command format is:
echo string
Read reads a string from the standard input and stores it in the following variable, with the command format:
Read variable name
Program execution, display Help information, prompt input corresponding parameters, and finally through a simple variable replacement, complete the renaming task this program. Although only 5 lines, but changed the behavior of the command, greatly enhance the use of MV. In practical applications, the reader can replace the command for this applet as needed.
Working with multiple files
On the previous basis, if you want to rename a batch of files, the following example is more useful.
::::::::::::::
mvs.sh
::::::::::::::
for s in $*; do
echo "File: $s"
echo "Please input new name: "
read t
mv $s $t
if [ $? -eq 0 ]; then
echo "OK!^o^"
else
echo "Error!"
fi
done
With the For loop, the command sequence processes all the files in the file list, in turn. $* represents all command-line arguments so that the file list can be specified by command parameters. The following if statement tests the execution state of the command, and 0 represents successful execution. After a simple deployment of 1, you can easily rename a file in batches by typing "MVS. jpg."
Deployment Scenarios
The program is completed and needs to be carefully deployed to facilitate installation and use.
Create a new bin directory in the user's home directory (skipped if established):
mkdir ~/bin
Add ~/bin to the path variable and add the following lines to the. bashrc file in the user's home directory:
Path=~/bin: $PATH
Export PATH
Move the tested program to the bin directory, change it to a simple, easy to remember name, and set the file properties:
CP mvs.sh ~/BIN/MVS
chmod 755 ~/bin/mvs
Later, you can directly use MVS to change the name of a batch of files.
MVS *.jpg
Although the examples presented in this article are simple, we sketch out some more common frameworks, which can be solved by a little modification to solve many more complicated tasks.