Compile all .c files in the current directory
! # / bin / the bash IF [-LT-$ # . 1 ]; the then # If an input parameter is less than a given $ # == C language in the argv [ 0 ] number of input parameters echo " Please file.c Follow up! " echo " EG: ./make.sh xxx.c " Exit Fi IF [[$ 2 = " debug " ]]; the then # debug if the third parameter is then, is compiling debugging FILE2 = $ . 1 echo $ {} FILE2 NAME = $ {FILE2%. *} # intercepting input file to be compiled. contents before such NAME = hello.c Hello echo " NAME " echo $ {NAME} GCC -g $ FILE2} {} -o $ {# NAME plus - G for gdb debugger gdb NAME} {$ the else # otherwise run directly FILE1 = $ . 1 NAME = *% $ {FILE1. } GCC $ {FILE1} - O NAME} {$ IF [-R & lt NAME $ {}]; the then echo " Test File IS Creature, Over! " ; . / $ {NAME} the else echo " Error! " Fi Fi
Designated for compiling C code debugging.
./make.sh file name (mandatory) debug (debug, optional)
#!/bin/bash for file in ./*.c do if [ -f $file ];then file=${file#./} target=${file%.c} echo "file ${file}" echo "target ${target}" gcc -o $target $file echo $target fi if [ -d $file ] ;then echo $file is dictionary fi done
exbot@ubuntu:~/shareWin/shell/20190925$ ./4.sh
file hello.c
target hello
hello
file world.c
target world
world
for file in . / * .P
This sentence means that looking for all .c files in the current directory prefixed with ./, such as hello.c after running becomes ./hello.c
file=${file#./}
This is to discard all characters before the file name in the leftmost ./. For example ./hello.c converted hello.c
file=/dir1/dir2/dir3/my.file.txt
{$ File # * /}: Deletion of a / the left and the string:. Dir1 / dir2 / dir3 / My File .txt $ { File ## * /}: delete the last / left and string:. My File .txt $ { File # *.}: delete the first string and the left:. File .txt $ { File ## * .}: delete the last one and left. string: TXT $ { File % / * }: delete the last character string / and right: / dir1 / dir2 / dir3 $ %% File {/ *}: delete the first / right and character :( null string) $ File% * {.}: delete the last character string and a right:. /dir1/dir2/dir3/my.file $ %% File * {.}: a first delete . its strings on the right: / dir1 / dir2 / dir3 / my
# Is removed on the left (on the left side of the keyboard # $ a) % on the right is removed (% keyboard on the right $) of a single symbol is a minimum matching; two symbols is the maximum matching $ { File : 0 : . 5 }: Extraction leftmost the 5 bytes: / dir1 $ { File : 5 : 5 }: extracting first 5 consecutive bytes 5 bytes right: / dir2
String replacement
{$ File / dir / path}: a dir replace the first path: / path1 / dir2 / dir3 / My. File .txt $ { File // dir / path}: replace all dir path: / path1 / path2 / path3 / my.file.txt
$ {} May use different variables for assignment state (is not set, null, non-null value):
{$ File -My. File .txt}: If $ File is not set, use My. File .txt as the return value. (Non-treated and non-null value null value) $ { File :. -My File .txt}: If $ File . Or not set to null value, the My File .txt as the return value. (Non-treated non-null value) $ { File + My. File .txt}: If $ File is set to null or non-null value, are used My. File .txt as the return value. (Without processing is not set) $ { File : + My. File .txt}: If $ File non-null value, then My. File .txt as the return value. (No processing time is not set and a null value) $ { File = My. File .txt}: If $ FileIs not set, use the My. File .txt as the return value, while the $ File assigned to My. File .txt. (Non-treated and non-null value null value) $ { File : = My. File .txt}: If $ File is not set or a null value, then My. File .txt as the return value, while $ File Assignment is My. File .txt. (Non-treated non-null value) $ { File My?. File .txt}: If $ File is not set, then My. File .txt output to STDERR. (Non-treated and non-null value null value) $ { File :? My. File .txt}: If $ File is not set or a null value, the My. File .txt output to STDERR. (Non-treated non-null value) $ {#} var variable length calculated value: $ {#File } to obtain 27 , as / dir1 / dir2 / dir3 / My. File .txt is 27 bytes
#/bin/bash for file in *.c do if [ -f $file ];then target=${file%.c} gcc -o $target $file echo $target fi if [ -d $file ] ;then echo $file is dictionary fi done
operation result:
exbot@ubuntu:~/shareWin/shell/20190925$ ./4.sh
hello
world