计算化学公社

 找回密码 Forget password
 注册 Register
Views: 7258|回复 Reply: 13
打印 Print 上一主题 Last thread 下一主题 Next thread

[Linux] Linux实用脚本以及bashrc配置收集

[复制链接 Copy URL]

106

帖子

1

威望

2048

eV
积分
2174

Level 5 (御坂)

跳转到指定楼层 Go to specific reply
楼主
本帖最后由 rtransformation 于 2017-4-15 21:27 编辑

距离自己上两个愚蠢的帖子:
http://bbs.keinsci.com/forum.php?mod=viewthread&tid=5172&extra=
http://bbs.keinsci.com/forum.php?mod=viewthread&tid=5358&extra=

过去一个多月了,实验室管理服务器的师兄面临毕业,只能我来管理管理服务器了,起初真的是懒得管,懒得学,现在却觉得特别好玩。

起初决定学tcsh的,后来发现bash才是最广的,学习资源也多,所以转投bash,两星期前萌发转到zsh上去的念头,各个节点也都配置好了,后来被自己的理性扼杀了,
为啥?
因为并没有什么用。

这段时间写了套自己用着舒服的bashrc,按照我自己的意愿给自己组的服务器归置了以下,主要写了个gaussian和gamess的脚本,也没什么高明的,贴出来抛砖引玉,
主要是希望大家在这个帖子下面分享分享自己平时能够提高自己运作效率实用脚本或功能,我也好好学习学习。

论坛里也很多高人写的各种脚本,我就不在这儿一一列举了,我发这个帖子主要想把大家的贡献集中起来。

(写的这脚本实验室成员也测试了挺久了,大体没啥问题,但是发现有时候算特别长的任务的时候,不能执行到脚本结尾。)

一、gaussian的计算脚本:
  1. #!/bin/bash
  2. export MOL=$1
  3. export FCHK=$2
  4. export SUBDIR=~+
  5. export GAUSS_SCRDIR=~/tmp/gaussian/$
  6. export GAUSS_EXEDIR=xxxxxxxxxxxxxxxx      #写自己的gaussian路径
  7. export USERNAME=`whoami`
  8. if [ null$MOL == null ]; then
  9. echo -e "\n\033[31m ************************************* \033[0m"
  10. echo -e "\033[31m Please input the name of a .com file! \033[0m"
  11. echo -e "\033[31m ************************************* \033[0m"
  12. exit
  13. fi
  14. full=`date --rfc-2822`                           #时间这儿,这样写,适合中文系统的同学想获得英文缩写。
  15. week=${full%,*}
  16. submit=`date "+%Y-%m-%d, $week, %T"`
  17. start=`date +%s`
  18. if [ -e $SUBDIR/$MOL.com ] ; then
  19. echo -e "\n *******************************************************"
  20. echo " Gaussian Task:"
  21. echo " Now calculating $SUBDIR/$MOL.com!"
  22. echo " *******************************************************"
  23. mkdir -p $GAUSS_SCRDIR
  24. cd $GAUSS_SCRDIR
  25. echo -e "\033[31m Now $SUBDIR/$MOL.com is still calculated in the background~ \033[0m"  >> ~/tmp/now.log
  26. $GAUSS_EXEDIR/g09 $SUBDIR/$MOL.com $SUBDIR/$MOL.log
  27. else
  28. echo -e "\n\033[31m ************************************************************************* \033[0m"
  29. echo -e "\033[31m Cannot find $SUBDIR/$MOL.com! Please check your PATH! \033[0m"
  30. echo -e "\033[31m ************************************************************************* \033[0m"
  31. exit
  32. fi
  33. if grep -i '%chk=' $SUBDIR/$MOL.com ; then
  34. if [ -e *.chk ] ; then
  35.   mv -f *.chk $SUBDIR/$MOL.chk
  36. fi
  37. else
  38. echo -e "\nYou didn't request the .chk file!"
  39. fi
  40. if [ -e *.wfx ] ; then
  41. mv -f *.wfx $SUBDIR/$MOL.wfx
  42. fi
  43. if [ -e *.wfn ] ; then                                           #这部分自己想要啥文件自己加就好
  44. mv -f *.wfn $SUBDIR/$MOL.wfn
  45. fi
  46. for ((k=31; k<=41; k++))
  47. do
  48. if [ -e *.$k ] ; then
  49. mv -f *.$k $SUBDIR/$MOL.$k
  50. fi
  51. done
  52. if [ $FCHK !="fchk" ] || [ null$2 == null ] ; then > /dev/null
  53. echo "You didn't request to produce the .fchk file!"   
  54. else
  55. if [ -e $SUBDIR/$MOL.chk ] ; then
  56.   formchk $SUBDIR/$MOL.chk $SUBDIR/$MOL.fchk
  57.   echo ".fchk file formed sucessfully"
  58. else
  59.   echo "So cannot form .fchk file!"
  60. fi
  61. fi
  62. rm -rf $GAUSS_SCRDIR
  63. echo -e "\n=========================================================================="
  64. echo "The temporary folder $GAUSS_SCRDIR of calculation was deleted"
  65. stop=`date +%s`
  66. time=`expr "$stop" - "$start"`
  67. end=`date "+%Y-%m-%d, $week, %T"`
  68. echo "**************************************************************************" >> $SUBDIR/$MOL.log
  69. echo File: $SUBDIR/$MOL.com >> $SUBDIR/$MOL.log
  70. if [ $time -le 60 ] ; then
  71. echo Time consuming: $time seconds >> $SUBDIR/$MOL.log
  72. elif [ $time -gt 60 -a $time -le 3600 ] ; then
  73. echo "Time consuming: `echo "scale=2;$time/60"|bc` minutes" >> $SUBDIR/$MOL.log
  74. elif [ $time -gt 3600 -a $time -le 86400 ] ; then
  75. echo "Time consuming: `echo "scale=2;$time/3600"|bc` hours" >> $SUBDIR/$MOL.log
  76. elif [ $time -gt 86400 ] ; then
  77. echo "Time consuming: `echo "scale=2;$time/86400"|bc` days" >> $SUBDIR/$MOL.log
  78. fi
  79. echo Submission time: $submit >> $SUBDIR/$MOL.log
  80. echo Ending time: $end >> $SUBDIR/$MOL.log
  81. echo "**************************************************************************" >> $SUBDIR/$MOL.log
  82. echo Temporary folder is $GAUSS_SCRDIR >> $SUBDIR/$MOL.log
  83. echo "=========================================================================="
  84. tail -25 $SUBDIR/$MOL.log
  85. echo "These last 25 lines are from file: $SUBDIR/$MOL.log"
  86. echo "=========================================================================="
  87. echo -e "\n\033[31m ************************************************************************************** \033[0m" >> ~/tmp/results.log
  88. echo -e "\033[31m Gaussian Task:\033[0m"                                        >> ~/tmp/results.log
  89. grep -q "Normal termination" $SUBDIR/$MOL.log
  90. if [ $? -eq 1 ] ; then
  91. echo -e "\033[31m $SUBDIR/$MOL.com !!!ERROR TERMINATED!!! at $end \033[0m"      >> ~/tmp/results.log
  92. else
  93. echo -e "\033[31m $SUBDIR/$MOL.com !!!NORMAL TERMINATED!!! at $end \033[0m"     >> ~/tmp/results.log
  94. fi
  95. echo -e "\033[31m ************************************************************************************** \033[0m" >> ~/tmp/results.log
  96. w|grep "$USERNAME"|head -n 1|sed 's/ //g' > /dev/null
  97. if [ $? -eq 0 ] ; then
  98. rm -f ~/tmp/results.log
  99. fi
  100. sed -i '/'$MOL.com'/d' ~/tmp/now.log
复制代码
主要能实现的功能就是:
1.运算成功结束后自动删除临时文件。
2.登录时会有提示哪些任务还在进行。
3.登录时会有提示哪些任务已经结束,哪个人物成功了,哪个任务失败了。
4.你在线时,如果有程序结束,会打印log文件最后25行,这在输入文件有问题时比较有用,这样就省的自己打开log文件查看了。
5.在log文件最后追加计算时间,提交及结束日期。
6.在log文件最后追加临时文件夹路径,非正常结束时方便定位。(脚本中没有把gaussian给临时目录弄的随机数字改成分子名称,主要是因为实验室有人喜欢弄同名文件)。
注:要实现2,3需要在.bashrc,或.bash_profile或.bash_login中加入:
  1. if [ -e ~/tmp/results.log ] ; then
  2. echo -e "\n\033[33m Tasks that have been already done:\033[0m"
  3. /bin/cat ~/tmp/results.log
  4. fi
  5. rm -f ~/tmp/results.log  > /dev/null
  6. if [ -s ~/tmp/now.log ] ; then
  7. echo -e "\n\033[33m Tasks that are still going on:\033[0m"
  8. /bin/cat -n ~/tmp/now.log
  9. else
  10. rm -f ~/tmp/now.log  > /dev/null
  11. fi
复制代码

二、gamess脚本(和gaussian大同小异,写好gaussian脚本后,这个几乎是马上就完成了,所以就主要贴不同的地方吧)
  1. #!/bin/bash
  2. export MOL=$1                    
  3. export NCPUS=$2                                             #gamess还得自己在外部指定核数,挺讨厌。
  4. export SUBDIR=~+
  5. export GMS_SCRDIR=~/tmp/gamess/$
  6. export GMS_EXEDIR=xxxxxxxxxxxxxxxxx      #写你的gamess路径
  7. export VERNO=00
  8. if [ null$MOL == null ]; then
  9. echo -e "\n\033[31m ************************************* \033[0m"
  10. echo -e "\033[31m Please input the name of a .inp file! \033[0m"
  11. echo -e "\033[31m ************************************* \033[0m"
  12. exit
  13. fi
  14. if [ null"$NCPUS" == null ] ; then
  15. export NCPUS=1
  16. fi
  17. if [ -e $MOL.inp ] ; then                                          #这儿自动创建路径的地方您要是不喜欢自己可以改改。
  18. dir=$(dirname `find $PWD -name $MOL.inp`)
  19. dirpart=${dir#*gamess}
  20. if [ -e $SUBDIR/$MOL.dat ] ; then                           #这个地方主要是删除同一任务上次的dat等文件,以确保运算进行。
  21.   rm -f $SUBDIR/$MOL.dat
  22.   echo -e "\n Files that produced by last calculation has been deleted, calculation is going on~"
  23. else
  24.   echo -e "\n There is no files that produced by last calculation, calculation is just going on~"
  25. fi
  26. echo -e "\n *******************************************************"
  27. echo " Gamess Task:"
  28. echo " Now calculating $SUBDIR/$MOL.inp!"
  29. echo " *******************************************************"
  30. mkdir -p ~/gamess/scratch/$dirpart/$MOL
  31. export INTGLDIR=~/gamess/scratch/$dirpart/$MOL
  32. mkdir -p $GMS_SCRDIR
  33. echo -e "\033[31m Now $SUBDIR/$MOL.inp is still calculated in the background~ \033[0m"  >> ~/tmp/now.log
  34. $GMS_EXEDIR/rungms $MOL $VERNO $NCPUS $GMS_SCRDIR $SUBDIR $GMS_EXEDIR >& $SUBDIR/$MOL.log
  35. else
  36. echo -e "\n\033[31m ************************************************************************* \033[0m"
  37. echo -e "\033[31m Cannot find $SUBDIR/$MOL.inp! Please check your PATH! \033[0m"
  38. echo -e "\033[31m ************************************************************************* \033[0m"
  39. exit
  40. 剩下的和gaussian大同小异。
复制代码

三、检查gaussian的log收敛情况的小脚本。
  1. #!/bin/bash
  2. export LOG=$1
  3. export PATH=~+
  4. exten=${LOG#*.}
  5. if [ $exten == "log" ] ; then
  6. if [ `/usr/bin/grep 'Converged' $PATH/$LOG|/usr/bin/head -n 1|/usr/bin/sed 's/ //g'` ] ; then
  7.   echo -e "\n Now Shows the CONVERGED condition of this calculation:\n"
  8.   /usr/bin/grep 'Converged' $PATH/$LOG -A4
  9.   times=`/usr/bin/grep 'Converged' $PATH/$LOG -c`
  10.   /usr/bin/grep -q "Normal termination" $PATH/$LOG
  11.    if [ $? -eq 0 ] ; then
  12.     echo -e "\n The molecule experienced \033[31m$times\033[0m times iteration during this calculation~\n"
  13.    else
  14.     echo -e "\n This molecule is ERROR terminated or the calculation is still going on with the \033[31m$times\033[0m th iteration~\n"
  15.    fi
  16. else
  17.   echo -e "\n There exists no CONVERGED information during this calculation~\n"
  18. fi
  19. else
  20. echo -e "\n Please input the right logfile name or make sure that you are inputting a logfile!\n"
  21. fi
复制代码

我这脚本里肯定有写的罗嗦的地方,希望大家批评指正,
希望大家多多分享linux实用脚本,以及其他跟计算化学相关的实用脚本,互相学习。



评分 Rate

参与人数
Participants 8
威望 +1 eV +45 收起 理由
Reason
shx + 5 谢谢分享
元江1994 + 5 好物!
ggdh + 5 谢谢分享
ZCSco + 5 GJ!
zsu007 + 5 赞!
sobereva + 1
978142355 + 5 谢谢
helpme + 15 谢谢分享

查看全部评分 View all ratings

50

帖子

0

威望

1491

eV
积分
1542

Level 5 (御坂)

2#
发表于 Post on 2017-4-15 23:40:43 | 只看该作者 Only view this author
zsh 怎么会没有什么用。
用过zsh 之后完全不想碰bash。

106

帖子

1

威望

2048

eV
积分
2174

Level 5 (御坂)

3#
 楼主 Author| 发表于 Post on 2017-4-16 10:44:08 | 只看该作者 Only view this author
ulosggs 发表于 2017-4-15 23:40
zsh 怎么会没有什么用。
用过zsh 之后完全不想碰bash。

那我再看看吧,哈哈

908

帖子

37

威望

5435

eV
积分
7083

Level 6 (一方通行)

4#
发表于 Post on 2017-4-16 12:24:31 | 只看该作者 Only view this author
个人感觉bash 学好awk 和 sed就好。
另外对写脚本有兴趣趁早学python
脚本一长,用bash就很痛苦了。写python 可以一口气写100行,然后再debug
写bash每写5行就得debug一次。

908

帖子

37

威望

5435

eV
积分
7083

Level 6 (一方通行)

5#
发表于 Post on 2017-4-16 12:30:27 | 只看该作者 Only view this author
大侠,能不能把脚本的运行效果贴出来啊。不然我得一行一行看代码来分析这个脚本是干嘛的。。

908

帖子

37

威望

5435

eV
积分
7083

Level 6 (一方通行)

6#
发表于 Post on 2017-4-16 12:47:09 | 只看该作者 Only view this author
本帖最后由 ggdh 于 2017-4-16 12:50 编辑

看明白了。原来是g09的wrapper,不错的设计,给出一些建议:
wapper最后加入一些接口:调用下面两个脚本(需要自己写,或者找别的资源)
1 inforgrab
如果正常结束,自动抓取常用信息,比如HOMO LUMO 总能量 激发能 偶极等
2 wfnana
调用multiwfn,自动运行常规波函数分析。

106

帖子

1

威望

2048

eV
积分
2174

Level 5 (御坂)

7#
 楼主 Author| 发表于 Post on 2017-4-16 12:53:21 | 只看该作者 Only view this author
ggdh 发表于 2017-4-16 12:47
看明白了。原来是g09的wrapper,不错的设计,给出一些建议:
wapper最后加入一些接口:调用下面两个脚本( ...

啊,好建议,以后加上,全部一体化。

106

帖子

1

威望

2048

eV
积分
2174

Level 5 (御坂)

8#
 楼主 Author| 发表于 Post on 2017-4-16 15:44:19 | 只看该作者 Only view this author
ggdh 发表于 2017-4-16 12:30
大侠,能不能把脚本的运行效果贴出来啊。不然我得一行一行看代码来分析这个脚本是干嘛的。。

本来想贴的,但是觉得太麻烦了。。。

106

帖子

1

威望

2048

eV
积分
2174

Level 5 (御坂)

9#
 楼主 Author| 发表于 Post on 2017-4-16 15:45:33 | 只看该作者 Only view this author
ggdh 发表于 2017-4-16 12:24
个人感觉bash 学好awk 和 sed就好。
另外对写脚本有兴趣趁早学python
脚本一长,用bash就很痛苦了。写pyt ...

是的,后来没再整zsh也是这个原因,有整shell的功夫,不如把时间用在好的语言上。

395

帖子

8

威望

3906

eV
积分
4461

Level 6 (一方通行)

石墨

10#
发表于 Post on 2017-4-16 19:58:57 | 只看该作者 Only view this author
ggdh 发表于 2017-4-16 12:47
看明白了。原来是g09的wrapper,不错的设计,给出一些建议:
wapper最后加入一些接口:调用下面两个脚本( ...

第一个原先用sed和gawk写过,sed抓取+gawk制表,方便是方便一点了,然而最后还是要打开gview......
自在飞花轻似梦,无边丝雨细如愁。

全自动反应动力学(ReaxFF、AIMD、NEP等)后处理工具网页版:http://cc-portal.xyz/reax_tools

395

帖子

8

威望

3906

eV
积分
4461

Level 6 (一方通行)

石墨

11#
发表于 Post on 2017-4-16 20:33:29 | 只看该作者 Only view this author
第三个脚本我写的版本::
  1. #!/bin/bash
  2. log=$1
  3. state=$( (grep -q 'Error termination' $log && echo Error) || (grep -q 'Normal termination' $log  && echo Normal) || echo Processing )
  4. time=$(grep -c 'Conver' $log)
  5. echo -e "The state of this job:$state\nOpt converged $time times\n$(grep -E '\ *(Displace|Force\ )' $log | tail -n 8)"
复制代码


评分 Rate

参与人数
Participants 1
eV +5 收起 理由
Reason
rtransformation + 5 谢谢分享

查看全部评分 View all ratings

自在飞花轻似梦,无边丝雨细如愁。

全自动反应动力学(ReaxFF、AIMD、NEP等)后处理工具网页版:http://cc-portal.xyz/reax_tools

50

帖子

0

威望

1491

eV
积分
1542

Level 5 (御坂)

12#
发表于 Post on 2017-4-17 01:28:48 | 只看该作者 Only view this author
rtransformation 发表于 2017-4-16 15:45
是的,后来没再整zsh也是这个原因,有整shell的功夫,不如把时间用在好的语言上。

https://github.com/robbyrussell/oh-my-zsh

zsh,copy 一份配置文件即可,不用花时间。
然后就能体会到巨大的区别了。

106

帖子

1

威望

2048

eV
积分
2174

Level 5 (御坂)

13#
 楼主 Author| 发表于 Post on 2017-4-17 07:56:49 | 只看该作者 Only view this author
本帖最后由 rtransformation 于 2017-4-17 09:08 编辑
ulosggs 发表于 2017-4-17 01:28
https://github.com/robbyrussell/oh-my-zsh

zsh,copy 一份配置文件即可,不用花时间。

装了这个了,然后我把bash 的prompt 仿照ohmyzsh 的ys 主题改了下

191

帖子

0

威望

524

eV
积分
715

Level 4 (黑子)

14#
发表于 Post on 2019-1-10 22:22:59 | 只看该作者 Only view this author
请问一下,第一个脚本,意思提交guassian计算的时候,还要运行脚本吗,脚本中备注要得到什么文件就输入什么文件,那个比如是要得到log ,chk,就输入*log,*chk吗?时间这儿什么意思?

本版积分规则 Credits rule

手机版 Mobile version|北京科音自然科学研究中心 Beijing Kein Research Center for Natural Sciences|京公网安备 11010502035419号|计算化学公社 — 北京科音旗下高水平计算化学交流论坛 ( 京ICP备14038949号-1 )|网站地图

GMT+8, 2025-8-12 20:02 , Processed in 0.247064 second(s), 21 queries , Gzip On.

快速回复 返回顶部 返回列表 Return to list