Presentation is loading. Please wait.

Presentation is loading. Please wait.

粒子物理与核物理实验中的数据分析 杨振伟 清华大学 第1讲:Linux环境下的编程.

Similar presentations


Presentation on theme: "粒子物理与核物理实验中的数据分析 杨振伟 清华大学 第1讲:Linux环境下的编程."— Presentation transcript:

1 粒子物理与核物理实验中的数据分析 杨振伟 清华大学 第1讲:Linux环境下的编程

2 内容 基本的Linux概念 Linux环境、shell和命令 一些简单而实用的例子 (!!!) Linux下的文件和目录
文件编辑器 (vi, emacs, less, more, pico, etc) Linux 环境变量和脚本 2019/6/3

3 什么是 Linux Linux: 一种操作系统内核
Ubuntu, debian, CentOS, SLC,… 开源、(几乎)免费 广泛用于科学计算、数据分析, … UNIX: 一种操作系统,与Linux类似 (命令行命令相同,但有时功能和用法会略有差别) 需要特别注意Linux和UNIX命令的细微差异,可能导致很不一样的结果 苹果操作系统(Mac OS X)是基于UNIX 的发行版 2019/6/3

4 大型科学研究广泛使用 Linux/Unix
Scientific Linux CERN(SLC) 当前发行版: SLC6.7 基于Redhat Enterprise Linux(RHEL)发行版 (下载iso文件) 对于个人电脑,推荐使用Ubuntu & Mac 2019/6/3

5 为什么使用 Linux (1) 源代码开放 有时可能需要你修改底层代码 提供了大量shell命令让生命更精彩
科研中可能会遇到的小问题(程序相关): 1.在某文件夹下有很多文件和子文件夹,需要将所有的.cpp文件中的yang_int改成yang_float 2.同样该文件夹,有很多文件中可能包含某个函数,比如Fit_Landau(),需要找到函数的定义及使用 3.查看某个文本文件a.txt一共有多少行 4.某程序,需要变更其中的参数进行多次运行。 对Linux而言就是小菜一碟!!! 2019/6/3

6 为什么使用 Linux (续) 源代码开放 有时可能需要你修改底层代码 提供了大量shell命令实现各种功能 命令可以组合组合拳(脚本)
如何知道有哪些命令? 怎么用? Search by Baidu, google, … Ask experts around you Read a Linux introductory book is necessary 练习: 如何获取Linux系统当前时间? 在屏幕上打印出当前月份。 提示: 内事不决 2019/6/3

7 一些有用的命令 Note: 1) Linux 下大小写敏感 ! 2) 命令可能会有多种参数 ! backup slides更精彩
pwd 显示当前目录 (print working dir) passwd 修改当前用户的密码 (change password) ls [-lahrt] 列出当前目录中的文件(list files in current directory) cd [dir] 进入指定目录 (change directory) cd,cd foo,cd ../ cd /home/yangzw/ mkdir dfoo 生成名为 dfoo 的子目录 (make a directory “dfoo”) rm foo 删除文件 foo(remove file “foo”) rmdir dfoo 删除空子目录dfoo (remove empty directory dfoo) cp foo bar 拷贝文件 foo 为另一文件 bar (copy file “foo” to “bar”) mv foo bar 重命名文件 foo 为 bar (rename foo as bar) man <command> 显示 command的说明 (display manual of command) history 列出最近用过的指令 (print the command used) du 显示当前目录空间大小(print current dir disk size) Note: 1) Linux 下大小写敏感 ! 2) 命令可能会有多种参数 ! 2019/6/3

8 命令行与shell shell:用户与操作系统交互的接口. 迥异于Windows,类似于 DOS 你需要熟悉终端(terminal)和命令行
提示符 目录 用户名 2019/6/3

9 文件和目录 Linux有自己的文件系统格式 最底层(root)的目录是: / 在 / 中存在很多子目录,可以用 ls 命令查看
目录存在层级结构 上层目录与下层目录之间用 / 分隔 例如 /home/yangzw, 是home是 / 的子目录,yangzw 是 home 的子目录 2019/6/3

10 目录的层级结构 如何访问你的主目录: cd ~ cd ~username / ← 根(root)目录 ← 操作系统设定 ← 系统管理员设定
usr/ bin/ home/ sys/ tmp/ ... chensm/ zhanghb/ yangzw/ ... WWW/ code/ thesis/ ← 操作系统设定 ← 系统管理员设定 ← 用户自己设定 Linux: public_html/ 如何访问你的主目录: cd ~ cd ~username 2019/6/3

11 绝对路径与相对路径 如何访问一个文件或者目录 ? 用户需要知道文件或目录的“路径” 路径有绝对与相对之分
绝对路径总是以 “/” 打头, 例如 /home/yangzw/hehe/hehe.dat 相对路径可以以 “~”, “.” 或 “..” 开头 “~” 是home的简写, 例如 ~/hehe/hehe.dat “.” 是当前目录的简写, 例如 ./hehe.dat “..” 是上层目录的简写, 例如 ../hehe.dat 2019/6/3

12 操作文件或目录 创建新目录 Make a new directory mkdir test
查看目录中的内容 list the files in a directory ls -l test 进入目录 change into a directory cd test 再创建一个目录 mkdir test2 查看当前目录中的内容 (ls) 可以看到刚创建的子目录 test2 将 test2 重命名为 test3 mv test2 test3 用 ls 查看当前目录中的内容 test3 注意可选参数 “-l” 2019/6/3

13 文件的所有者属性与访问权限 “ls -l” 给出了更多的信息。这都是什么东东? “r”, “w”, and “x” 什么意思?
它们代表文件的访问属性: r: readable w: writable x: executable Q: 如何更改这些属性? Hints: chmod, chown “yangzw” 和 “staff”? 它们表示文件的所有者属性: user group other 2019/6/3

14 文件的所有者属性与访问权限 “ls -l” 给出了更多的信息。这都是什么东东? 访问权限: 可读(r)、可写(w)、可执行(x)
所有者属性: 所有者(user)、所有者同组成员(group)、其它用户(other) ”d” 表示这是个目录!!! 用 chmod 可更改访问权限 chmod 755 hehe.txt (r:4 w:2 x:1) rwx:7 rw:6 rx:5 用 chown 可更改所有者属性 chown yangzw:lhcb <filename> 2019/6/3

15 操作文件或目录 (II) 文件层级很多,怎样从某个目录快速回到home目录? 怎样知道当前处于哪个目录? 怎样进入刚离开的目录?
工作时可能有很多子目录,需要经常切换目录。有什么好办法能够快速回到某个特定的工作目录? 2019/6/3

16 pushd 和 popd APUE 是工作目录。 不管你走到何方 popd会带你回家 我回来了。。。 2019/6/3

17 查看文件内容 例如 “~/.bash_profile”, 如何查看其内容? “ls” 显示不出来这个文件 … ls -a 可以显示隐藏文件 (以 ‘.’ 开头的文件) 1. cd 你的home目录 2. 利用这几个命令 cat, tail, head, more and less 查看文件 .bash_profile cat .bash_profile # print the file to the screen head .bash_profile # print the first 10 lines head -20 .bash_profile # print first 20 lines tail .bash_profile # print last 10 lines tail -30 .bash_profile # print last 30 lines more .bash_profile # use more to look at the file less .bash_profile # use less to look at the file 注意:输入”q”可以从more或less中退出。 在 less 中: up 、down 箭头可以上下浏览 / 后面跟上关键字向下进行搜索 ?后面跟上关键字向上进行搜索 2019/6/3

18 文本编辑 存在三种不同的编辑器: 文本编辑效率对写程序非常重要! 1. vi/vim 2. emacs
3. 其他 (gedit, pico, nano, …) 2019/6/3

19 Emacs, Vi nano(pico): 简单文本编辑 emacs:很好很强大 打开文件 nano [filename] 保存文件 ^o
退出文件 ^x emacs:很好很强大 打开文件 emacs [filename] 保存文件 ^x ^s 退出文件 ^x ^c vi(vim):古老,不过也很强大 打开文件 vi [filename] 保存文件 :w (注意:是输入冒号然后输入w或q或q!) 退出文件 :q 不存退出 :q! 注:vi有两种模式,命令模式和输入模式 按小写字母”i”进入输入模式,按”esc”键进入命令模式 在命令模式中可以输入命令很方便的进行编辑修改 讲义最后列了一些vi的常用命令 2019/6/3 熟练使用任何一种编辑器都可以极大提高工作效率,建议多多练习。

20 替换或提取文本内容 科研中经常遇到这类问题 有什么好办法不? 2019/6/3

21 sed 和 awk 有用的例子: 可在命令行中对文件内容进行替换或提取
sed –e ‘s/float/double/g’ <filename> 将float全局替换成double sed –e ‘s/A/B/g’ <filename> 将A全局替换成B awk ‘{print $1}’ <filename> 将文件第一列打印到屏幕 [默认用空格分隔不同的列] awk –F‘=’ ‘{print $1}’ <filename> 利用 ‘=’ 符号分隔列 awk '{s+=$1}END{print "sum = " s}' <filename> 对第一列的数字求和 请尝试利用sed和awk对自己创建的文件进行一些操作练习,例如 hehe.C, hehe.txt 和 hehe2.txt. 2019/6/3

22 更多命令行命令的例子 grep <string> hehe.txt > MyStrFile
./myBin >& myBin.log & tail –f myBin.log cat file2 >> file1 export MYVAR=val; ./myBin >& mylog & tail –f mylog ls /usr/local/bin/?v* 搜索文件hehe.txt中的字符串string并将结果保存成文件MyStrFile 执行文件myBin,将标准输出和标准错误重定向至文件myBin.log。最后的&符号表示让程序在后台运行。 将 file2 的内容添加到 file1 的文件末尾 一行运行多个命令,中间用分号隔开 查看/usr/local/bin中所有文件名第二个字符是v的文件。这是正则表达式的一个例子 正则表达式(Regular Expression) 很强大! 2019/6/3

23 PATH: 一个重要的环境变量 我们已经用过很多命令,但是 … 操作系统怎么知道我们想用的命令在哪里?
如果系统在不同目录中存在多个相同名称的命令怎么办? PATH: 一个重要的环境变量 2019/6/3

24 环境变量 shell中有很多环境变量。查看所有环境变量: printenv or env 最重要的环境变量之一:
PATH 该环境变量的值是一个路径列表,路径之间用“:”分隔。 当我们让系统执行某个命令时,系统会按照这个路径列表 搜索,直到找到为止。如果某个命令所在目录不在PATH中, 则执行该命令的时候需要加上路径,例如 /sbin/ifconfig instead of ifconfig 用 which 命令可以查看某个命令位于何处或是否存在。该命令从$PATH 中的第一个目录开始搜索,例如 2019/6/3

25 使用环境变量 export myVar=value echo $PATH unset myVar 设置环境变量 变量名 变量值
查看环境变量的值 注意前面要加 $ echo是个shell命令 unset myVar 取消环境变量 2019/6/3

26 使用环境变量(II) 有时可能需要追加些东西到某个变量中,例如 PATH
export PATH=<new path to add>:$PATH “command not found” 仅表示无法在 $PATH 中找到需要的命令,不代表操作系统中不存在该命令 注意:这里用的都是 bash 的例子。对于 tcsh 而言,语法有差异,但功能类似。 2019/6/3

27 什么是脚本?为什么? Linux命令的基本原则是一个命令实验一个简单的任务或功能。但是… 你的要求可能很高很复杂
有时你可能需要重复运行很多类似的程序代码 有时 … Shell 是活雷锋 … 2019/6/3

28 什么是脚本?为什么? 将多个shell命令组合到一起  shell 脚本 脚本可以实现复杂功能或简化很多重复工作,有助于你
 更高效高产  腾出时间做更有用的事情 2019/6/3

29 脚本入门 脚本 test.sh: backup slides 更精彩 … 1) 第一行以 “#!”开头,后面跟随所用shell的绝对路径
#!/bin/bash # This is a simple test shell script echo “Hello world!” date echo $PWD backup slides 更精彩 … 1) 第一行以 “#!”开头,后面跟随所用shell的绝对路径 2) 其他以 “#” 开头的行为注释行 (注意“其他”二字) 3) 可以把任何需要的命令行命令放到脚本中 执行脚本 test.sh: source test.sh 或 ./test.sh # 注意保证 test.sh 具有 “x” 属性 # chmod u+x test.sh 2019/6/3

30 总结 什么是Linux 常用命令 文本编辑器: vim, emacs, 其他. shell, 环境变量, 脚本和脚本编程 2019/6/3

31 作业 在$HOME目录中创建一个目录MyTest; 进入目录MyTest,然后创建三个子目录和五个文件: 子目录: dir1,dir2, dir 文件: file1, file2, index1.htm, index2.htm, test1.txt 写一个脚本myscript.(c)sh实现下列功能 1) 在屏幕上显示当前时间 2) 在屏幕上显示当前路径,你的用户名和所用的shell 3) 显示环境变量LANG 和 DISPLAY的值 4) 估计当前目录所用的磁盘空间大小 5) 停顿5秒钟然后再打印当前时间 写一个脚本 looptest.(c)sh 实现下列功能: 1) 对于作业1中创建的目录和文件,修改器访问权限,使得:你自己拥有读写执行权限,其他所有人仅拥有只读权限。 2) 将作业1中htm文件重命名为html文件 选作题目:某文本文件内有如下内容,用脚本统计总的事例数 This is a log file 100: abc 100 events processed cde 120 events processed This is end of the file 2019/6/3

32 Backup slides 2019/6/3

33 Solutions to four problems
find . -name "*.cpp" -exec sed -i 's/float/double/g' {} \; Problem 2: grep -srn "Fit_Landau" /home/yangzw/mywork Problem 3: awk '{s+=$1}END{print "sum = " s}' test1.txt Problem 4: use shell script 2019/6/3

34 一些有用的命令 (cont.) 查看文件: 压缩或解压缩文件
tail <file> look at the end of a file head <file> look at the start of a file cat <file> print all contents of a file to terminal more <file> file viewer (SPACE for page down, q to quit) less <file> file viewer (more versatile than more) 压缩或解压缩文件 gzip <file> zip a file up tar cvf somefile.tar <directory> archive files or directory trees tar xvf somefile.tar unpack a tar file tar cvzf somefile.tgz <directory> make a tar file and zip it up tar xvzf somefile.tgz unpack a zipped tar file 2019/6/3

35 一些有用的命令 (cont.) ps report a snapshot of the current processes.
kill kill process with id=345 ./foo execute the file foo ctrl-c interrupt current process ln –s <file> <linkname> make a symbolic link for a file locate <file> find files by name find . –name <file> find file by name in current directory grep <pattern> <file> print lines matching a pattern sed –e “s/str1/str2/g” foo > bar stream editor for filtering and transforming text 2019/6/3

36 一些有用的命令(4) chmod 755 <file> change file permission (1:x 2:w 4:r 5:rx 7:rwx) chmod ug+x foo append x permission to owner and group members diff file1 file compare file1 and file2 tar –zcvf 1.tgz file1 file2 archive and compress file1 and file2 to 1.tgz tar –zxvf 1.tgz extract 1.tgz gcc test.c –o try compile C program to get executive try1 g++ test.cpp –o try compile C++ program to get executive try1 date display current time sleep wait for 10 seconds wc [–lw] file count how many lines/words in file echo “Welcome to Linux World!” print the string on screen file file determine file type man <command> show manual page for command man –k <keyword> show manual page according to keyword 2019/6/3

37 shell脚本中的循环 “for” and “while”
#!/bin/bash # Another test shell script ####for循环#### for i in `ls /home/yangzw` do echo $i done ####while循环### num=1 DIR=“testDir” while (( $num < 5 )) if [ -d $DIR$num ]; then echo “$DIR$num exist!!“ else mkdir $DIR$num fi let num+=1 #!/bin/tcsh # Another test shell script ####for循环#### foreach i `ls /home/yangzw` echo $i end ####while循环### num=1 DIR=“testDir” while ( $num < 5 ) if ( -d $DIR$num ) then echo “$DIR$num exist!!“ else mkdir $DIR$num endif set num=`expr $num +1` 2019/6/3 脚本中变量和循环的例子: bash vs tcsh

38 如何登陆Linux服务器 首先需要有账号和密码 终端登陆个人电脑:输入账号密码即可 也可以远程登陆服务器(或个人电脑) 2019/6/3

39 登陆远程服务器或文件传输 绝大部分Linux操作系统提供 ssh/scp 用于登陆远程服务器或在本地与远程之间传输文件
> ssh [options] (or ip address) > ssh > scp [options] <source> <target> > scp hehe.txt server.hep.tsinghua.edu.cn:./mytxt/ 注意:[options] 表示可加上某些可选参数 本地文件 远程服务器 远程服务器的目录(相对路径) 2019/6/3

40 备用(vim常用指令) 1. 显示行号 :se nu 2. 移动光标到第5行 :5 3. 移动光标到行首 ^ 4. 移动光标到行尾 $
5. 移动光标到文件头 gg 6. 移动光标到文件尾 G 7. 向后移动3个字 3w 8. 向前移动4个字 4b 9. 删除光标所在字 dw 10. 删除光标所在字符 x 11. 删除行 dd 12. 删除光标后3行 3dd 13. 删除光标至行尾 D 14. 用某字母(如”k”)替换光标所在字符 r k 15. 向下新增一行 o 16. 向上新增一行 O 17. 复制光标所在行 yy 18. 将复制的行粘贴到光标所在行下方 p 19. 将复制的行粘贴到光标所在行上方 P 20. 查找字符串”Abc” /Abc 21. 全局替换“Abc”为“ABC” :%s/Abc/ABC/g 22. 将3-9行的”Abc”替换为”ABC” :3,9s/Abc/ABC/g 23. 将3-6行复制到第9行 :3,6 co 9 24. 将3-6行移动到第9行 :3,6 m 9 25. 删除3-6行 :3,6 d 行行首加上”ABC” :3,6s/^/ABC/g 行行首加上”//”,即C++注释 :3,6s/^/\/\//g 行行尾添加”ABC” :3,6s/$/ABC/g 29. 将光标的下一行连接到光标所在行 J 30. 将光标所在处字母变更大小写 ~ 31. 取消操作(undo) u 32. 重复操作(redo) . 33. 全文加亮光标当前变量 gd 34. 保存文件 :w 35. 保存退出 :wq 36. 不保存退出 :q! 37. 进入输入模式 i 38. 进入命令模式 ESC 39. 在行首进入输入模式 I 40. 在行尾进入输入模式 A 41. 删除光标所在括号内字符,如圆括号 di( 【delete in ( )意】 42. 从光标处删除至某字母如X dtX 【delete to X意】 43. 从光标处复制至某字母如X ytX 44. 删除含关键字keyword的行 :g/keyword/d :g!/keyword/d (不删除) 2019/6/3


Download ppt "粒子物理与核物理实验中的数据分析 杨振伟 清华大学 第1讲:Linux环境下的编程."

Similar presentations


Ads by Google