notes

${} 和 $[]

$[expression] can calculate easily.
${expression} can tell us the value of a element in array.

eg

val1=23
val2=24
echo $val1+$val2
echo $[$val1+$val2]        #it is equal to `echo $(( $val1+$val2 )`

[edemon@CentOS workspace]$ ./incalc.sh 
23+24
47

assign values to array.

array=(`echo {1..10}`)
echo ${array[0]}" "${array[9]}

[edemon@CentOS workspace]$ ./array.sh 
1 10

evolve:

array=( {1..10} )
echo ${array[0]}" "${array[9]}

[edemon@CentOS workspace]$ ./array.sh 
1 0

for loop with C style in bash shell

# bash 风格
for i in {1..10} ; do
    echo -n $i 
done
echo

# The Loop With C Style
for (( i=1;i<=10;i++)){
    echo -n $i 
}
echo 

[edemon@CentOS workspace]$ ./Cstyle.sh 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10

the evolution comes from (( )) and { }

for {}

it can shorten command line.
cp f1.{txt,bp} # equal to cp f1.txt f1.bp
new build and edit three files:file1, file2 and file3
vim -O file.{1,2,3}

the connection effect of backslash

follow codes can show backslash’s connection effect.

[edemon@CentOS fileDir]$ echo {a,b,c} :{1,2}
a b c :1 :2
[edemon@CentOS fileDir]$ echo {a,b,c}:{1,2}
a:1 a:2 b:1 b:2 c:1 c:2
[edemon@CentOS fileDir]$ echo {a,b,c}{1,2}
a{1,2} b{1,2} c{1,2}
[edemon@CentOS fileDir]$ echo {a,b,c}.{1,2}
a.1 a.2 b.1 b.2 c.1 c.2
[edemon@CentOS fileDir]$ echo {a,b,c}.{txt,pdf}
a.txt a.pdf b.txt b.pdf c.txt c.pdf
touch `echo {a,b,c}.{txt,pdf}`

create new files we want instantly.
a.pdf a.txt b.pdf b.txt c.pdf c.txt

for (( ))

(( )) allow us to write C style operations. such as, (( val++ )), (( val = $1>$2?$1:$2 ))

Practice

count the number of files. (and got executing time.)

#! /bin/bash
start=`date +%s`
FILE=0
for file in /usr/*;do
FILE=`expr $FILE + 1`;
done
echo "/usr: "$FILE

FILE=0
for file in /usr/bin/*; do
FILE=`expr $FILE + 1`;
done
echo "/usr/bin: "$FILE

FILE=0
for file in /{,usr/}bin/*; do
FILE=`expr $FILE + 1`;
done
echo "{usr}/bin: "$FILE
end=`date +%s`
interval=`expr $end - $start`
echo $interval

————————————————————————————————————————

/usr: 12
/usr/bin: 1764
{usr}/bin: 1876
8

evolve:

#! /bin/bash
start=`date +%s`
ls -l /usr |grep '^[-,d,c,b,l,s]'| wc -l

ls -l /usr/bin |grep '^[-,d,c,b,l,s]'| wc -l

ls -l /{,usr/}/bin |grep '^[-,d,c,b,l,s]'| wc -l

end=`date +%s`
interval=`expr $end - $start`
echo $interval

————————————————————————————————————————

12
1764
1876
0

evolve again:
# ls $dir | wc -l

calculate the greatest common divisor

ARGS=2
if [ $# -ne "$ARGS" ]
then
    echo "Usage: `basename $0` first-number second-number"
    exit 1
fi
gcd ()
{
    dividend=$1
    divisor=$2 
    remainder=1 
    while (( $remainder != 0 ));do
        remainder=`expr $dividend % $divisor`
        dividend=$divisor
        divisor=$remainder
    done
}
gcd $1 $2
echo $dividend

copy all files in folders qt_pro1, qt_pro2, … ,qt_pro12 into folder my_qt.

#!/bin/bash
for it in `ls |grep qt_pro`; do
echo  $it
cp  -r $it/*  my_qt/
echo "...done"
done

delete all other files except Calender and rm.sh

[edemon@CentOS public]$ ls
Calender     libexpat.so.1     libgobject-2.0.so.0  libm.so.6           librt.so.1       libXau.so.6       libXext.so.6
cp.sh        libgcc_s.so.1     libgthread-2.0.so.0  libpthread.so.0     libselinux.so.1  libxcb-dri2.so.0  libXfixes.so.3
libc.so.6    libglapi.so.0     libicudata.so.51     libQt5Core.so.5     libstdc++.so.6   libxcb-glx.so.0   libXxf86vm.so.1
libdl.so.2   libglib-2.0.so.0  libicui18n.so.51     libQt5Gui.so.5      libX11.so.6      libxcb.so.1       read
libdrm.so.2  libGL.so.1        libicuuc.so.51       libQt5Widgets.so.5  libX11-xcb.so.1  libXdamage.so.1   run.sh

shell script:

#! /bin/bash
# rm.sh
for file in `ls` ;do 
    if [ $file != 'Calender' -a $file != 'rm.sh' ]; then
        rm $file
    fi
done

clear content in file.

cat /dev/null > filename

the number of parameters.

more precisely, the number is for the parameters which are passed to script.
the number of parameters of shell script is different to the number of paramters for C programs. The difference is whether take care of the script name.

C:

#include 

int main(int argc,char **argv){
    printf("argc %dn",argc);
    return 0;
}
/*
[edemon@CentOS tmpDir]$ ./run hello wolrd
argc 3
*/

shell:

# !/bin/bash
echo "length: "$#
echo "first: "$0
echo "second: "$1
[edemon@CentOS tmpDir]$ ./run.sh hello world
length: 2
first: ./run.sh
second: hello

find files in subdirectory recursively

#! /bin/bash
shopt -s globstar   #enable option globstar
for file in **; do
    echo $file
done

result:

[edemon@CentOS recurseTest]$ ./recFile.sh 
file1.txt
file2.txt
recFile.sh
subdir
subdir/childDir
subdir/childDir/file.txt
subdir/files
subdir/files2

read and save the first two lines content in file.

code block here has important effect.

file="./read"
{
read line1
read line2
} < $file
echo $line1
echo $line2

save multiple results.

script:

{
echo "this is an I/O redirect file."
echo time is `date` 
}> file

result:

[edemon@CentOS workspace]$ cat file
this is an I/O redirect file.
time is Mon Apr 3 14:29:42 CST 2017

test whether a command exists.

type $1 >& /dev/null
if (( $? != 0 )) ;then
    echo $1 is not a legitimate command
else echo $1 is a legitimate command.
fi

result:

[edemon@CentOS workspace]$ ./type.sh ls
ls is a legitimate command.
[edemon@CentOS workspace]$ ./type.sh lsl
lsl is not a legitimate command

a interesting phenomenon is that null parameter is regarded as right instruction.

[edemon@CentOS workspace]$ ./type.sh
is a legitimate command.

absolutely it’s right, because we can click down Enter and nothing wrong happens.

delete the first line in file.

[edemon@CentOS workspace]$ cat file
this is an I/O redirect file.
time is Mon Apr 3 14:29:42 CST 2017

tip: awk use $0 to print all fields.

cat file | awk '{if (!match($1,"^this")) printf("%s"),$0 }' > file

convert all characters to uppercase or lowercase

[edemon@CentOS workspace]$ ./upOrLower.sh 
origin text:         time is Mon Apr 3 14:29:42 CST 2017
first upper case:     Time is Mon Apr 3 14:29:42 CST 2017
all upper case:         TIME IS MON APR 3 14:29:42 CST 2017
first lower case:     time is Mon Apr 3 14:29:42 CST 2017
all lower case:         time is mon apr 3 14:29:42 cst 2017
[edemon@CentOS workspace]$ cat upOrLower.sh

shell script:

#!/bin/bash
text=`cat file`
echo -e "origin text:tt "$text
echo -e "first upper case:t "${text^}
echo -e "all upper case:tt "${text^^}
echo -e "first lower case:t "${text,}
echo -e "all lower case:tt "${text,,}

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x