Writing code on a day when Mercury is retrograde is actually a shell combat task two weeks ago, but because I have been procrastinating and procrastinating, I did not finish it until this afternoon, thank me here group leader , his code is really weird (really unexpected)
First of all, the requirements of this actual combat are as follows: 1.The file names of this bunch of pictures are non-consecutive numbers 2.Change the filenames of this bunch of pictures to be consecutive 3.The original order of the pictures cannot be changed
E.g:
In order not to change the order of the pictures, save the sorted pictures to another folder
First edition:
#!/bin/bash cd /home/computer/shell/practice/demo a=1 for file in $(ls) do name=${file%.*} NUM[a]=$name ((a=a+1)) echo "$name" done for (( i=1; i < a; i++));do ischanged=false for ((j=a-1; j> i; j--));do if [ ${NUM[j]} -lt ${NUM[j-1]} ];then temp=${NUM[j]} NUM[j]=${NUM[j-1]} NUM[j-1]=$temp ischanged=true fi done if [ ischanged = "false" ] ;then break; fi done #for((i=1;i<a;i++)); #do # echo "${NUM[$i]}" #done for file in $(ls) do mingzi=${file%.*} for((x=1;x<a;x++)); do if [ $mingzi == ${NUM[$x]} ] then new=`printf "%05d\n" $x` cp $file /home/computer/shell/practice/demo_new/$new.jpg fi done done
It may be that I have learned a lot of C language. The first reaction is to write a sort, sort the collected file names and then output them, so I wrote a bubble sort. But the disadvantage is obvious. First of all, there is a lot of code, which is very annoying; and the sorting time is very long. If there are many pictures, it will be very slow. Then group leader Just say there is no need to sort (this is really impossible to think of)
second edition:
#!/bin/bash cd /home/computer/shell/practice/demo a=1 for file in $(ls | sort -n) do name=${file%.*} echo "$name" new=`printf "%05d\n" $a` cp $file /home/computer/shell/practice/demo_new/$new.jpg ((a=a+1)) done
The effect is as follows
After thinking about it for a long time, when I checked the data, I found that sort can be sorted, even numerically, so I had an idea. finished writing, group leader Said not bad, so the end of the flower.
Leader's code:
#!/bin/bash cnt=0 for((i=0;i<20000;i++)) do if test -e JPEGImages/$i.jpg then ((cnt++)) cp JPEGImages/$i.jpg jpeg_successed/$cnt.jpg else echo "file $i.jpg does not existed" fi done
Because I'm really curious about what kind of situation can I not sort, so I'm coming group leader The code of the team leader is worthy of being the team leader. He wrote it in less than 13 lines, and even gave a hint to lose. But there is also a disadvantage. If there are only two pictures, one is called 1.jpg and the other is called 10000.jpg, it will take a long time.
Actual combat is used to learn knowledge, and this time is no exception
1. sort of shell
shell sort is an extension to insertion sort, but much faster than insertion sort.
Options | illustrate |
---|---|
-b | Ignore white space before each line |
-d | Only consider spaces and alphabetic characters, numbers |
-f | Ignore letter case |
-m | Merge already sorted files without sorting |
-n | Numeric comparison based on strings |
-o | Write the sorted results to a file |
-r | Sort in reverse order (the default sorting of sort is from top to bottom, from small to large, and the -n option can be used to flip the sorting) |
-t | Specifies the separator character to use when sorting |
-k | Specify the column (column, field) to be sorted |
-u | ignore identical lines |
This actual combat uses -n among them, for example:
Detailed explanation of sort command in shell
2. Get the file name and suffix of the shell
filename without directory
#Code: file="thisfile.txt" echo "filename: ${file%.*}" echo "extension: ${file##*.}" #output: filename: thisfile extension: txt
filename with directory
var=/dir1/dir2/file.txt echo ${var##*/} file.txt #extract suffix echo ${var##*.} txt #Extract filename without suffix, in two steps tmp=${var##*/} echo $tmp file.txt echo ${tmp%.*} file #Extract directory echo ${var%/*} /dir1/dir2
3. test of shell
There is a test -e in the code of the group leader, but I have not learned this command, so I query.
Options | illustrate |
---|---|
-e | The [file name exists] (commonly used) |
-f | Whether the [file name] exists and is a file (commonly used) |
-d | Whether the [file name] exists and is a directory (commonly used) |
-b | Whether the [file name] exists and is a block device |
-c | Whether the [file name] exists and is a character device |
-S | Whether the [file name] exists and is a Socket file |
-p | Whether the [file name] exists and is a FIFO (pipe) file |
-L | Whether the [file name] exists and is a linked file |
4. The ` symbol of shell
``Inside is usually a shell command, before executing the script, this command will be executed first, which is equivalent to a function call
of shell syntax , ' ' , {},``, ,'',(),$(()) four grammatical meanings