嵌入式开发记录-day03 linux软硬链接、makefile、shell脚本
1、新的文本编辑器gedit
可以在命令行中启动,直接输入:gedit "filename"
安装:sudo apt-get install gedit-plugins
文本编辑器与vi编辑器类似;
2、gedit常见快捷方式:
CTRL-Z:撤销
CTRL-C:复制
CTRL-V:粘贴
CTRL-T:缩进
CTRL-Q:退出
CTRL-S:保存
3、linux文件链接:
1、硬链接:使用inode节点,硬链接十多个文件指向同一个indoe的ID号;
// 假设当前文件下有可执行文件 hello ./hello // 输出本应该是hello world ln hello hello1 // 创建hello文件的链接文件hello1(类似快捷方式) ln hello hello2 // 创建hello文件的链接文件hello2 ll -i hello* // 查看生成hello链接节点信息 ./hello ./hello1 ./hello2 // 以上的执行等效
4、硬链接文件,不管修改源文件还是链接文件,所有文件都会被修改;
链接文件可以独立于源文件存在、运行;
硬链接不能链接文件夹;
5、符号链接(软连接):
类似于windows上的快捷方式,运行链接文件,会转去运行源文件;
软连接创建文件需要使用绝对路径,否则只可以在源文件所在路径中使用,拷贝到其他目录会运行不了;或者找不到源文件;
// 创建连接前 默认hello.c存在 gcc hello.c -o hello ln hello hello1 // 创建一个硬链接,类似于拷贝文件 ln -s hello hello2 // 创建一个软连接,类似创建快捷方式(文件小很多) ll -i hello*
// 使用绝对路径创建软连接 ln -s /home/liu/hello hello3 ln -i hello* // 查看软连接文件,会看到附带信息 cp -d hello3 test/ // 拷贝hello3到目录test下去,带有连接属性 cd test/ ./hello3 // 此时可以执行
6、GCC编译源文件
1、之前安装gcc的时候有提到过,编译单一文件时的例子;
2、编译多个文件时,有点类似,但是工作量大,可借助make工具
// 假设已经存在多个源文件、头文件 // input.c input.h // sum.c sum.h // main.c // 在手动编译的时候,需要 gcc main.c input.c sumc -o main // 生成目标文件main ./main // 运行下
这样做是可以的,但是每次运行所有的文件都会编译;源文件数量大了以后,特别费时;
所以,就想,编译只修改过的文件,没有修改的不会编译;因此就引入make工具的Makefile
7、Makefile
// 目标: 依赖对象 // 首先会查找依赖对象,没有依赖对象,则编译 main: main.o input.o sum.o gcc -o main main.o input.o sum.o // 生成main依赖main.o.... main.o:main.c // main.o依赖main.c gcc -c main.c // 编译main.c input.o:input.c gcc -c input.c sum.o:sum.c gcc -c sum.c clean: // 在清理时,make clean rm *.o rm main
升级操作:
1 // 使用变量代替繁琐的 2 Objects=main.o input.o sum.o 3 main: $(Objects) 4 gcc -o main $(Objects) 5 main.o:main.c 6 gcc -c main.c 7 input.o:input.c 8 gcc -c input.c 9 sum.o:sum.c 10 gcc -c sum.c 11 12 name=zzk 13 curent=$(name) 14 name=liubx // 变量的真实值,取决于最终的修改的值; 15 print: 16 @echo curenc:$(curent)
:= 这个不会取最后的值,=会取最后的值;?=前面若是幅值了,使用前面的,若没有使用的值;
+=追加的方式加进来;
使用模式规则改进Makefile
Objects=main.o input.o sum.o main:$(Objects) gcc -o main $(Objects) %.o:%.c gcc -c $< // 目标做对应的依赖文件 // 加上关键词后,有文件存在,也不会运行失败 .PHONY:clean: // 伪目标,若是有文件存在,运行可能失败; rm *.o rm main
8、shell脚本
1 #!/bin/bash 2 // 输出交互 3 echo "hello world" 4 // 文件命名hello.sh 5 chmod 777 hello.sh // 增加执行权限 6 7 // 交互输入 8 echo "input your name:" 9 read name 10 echo "your name:"$name 11 12 // 多参数输入 13 read -p "input your age and weight" age height 14 echo "your age=$age, height=$height" 15 16 //// 17 echo "input two num" 18 read -p "first num" first 19 read -p "second num " second 20 sum=$(($first+$second)) 21 echo "$first+&second= $sum" 22
test命令
1 #!/bin/bash 2 echo "input filename if exist" 3 read -p "please input filename" filename 4 test -e $filename && echo "$filename exist" || echo "$filenme no exist" 5 6 /////查看两个输入字符串是否相等 注意字符串与&&等符号之间的空格 7 echo “input two num” 8 read -p "first str" str1 9 read -p "second str" str2 10 test $str1==$str2 && echo "str1=str2" || echo "str1!=str2"
[]判断符
#!/bin/bash echo "input filename if exist" read -p "please input filename" filename test -e $filename && echo "$filename exist" || echo "$filenme no exist" /////查看两个输入字符串是否相等 echo “input two num” read -p "first str" str1 read -p "second str" str2 [ "$str1"=="$str2" ] && echo "str1=str2"||"str1!=str2" // [ 和字符串之间需要空格,括号内的字符串需要加引号
默认变量
$0~$n 一次表示第n个变量,$# 最后一个参数;$@ 参数的内容,命令行后跟的参数
/hello.sh ab cd e f

浙公网安备 33010602011771号