Bash
Table of Contents

Bourne-Again SHell manual

delete files excpet for some files

[tangzeyuan@mu03 03]$ ls
ase-sort.dat  CHGCAR   DOSCAR    IBZKPT  KPOINTS  OSZICAR  PCDAT   POTCAR  test03.o892   vasp.out     WAVECAR
CHG           CONTCAR  EIGENVAL  INCAR   mu03.py  OUTCAR   POSCAR  REPORT  vasp-mu03.sh  vasprun.xml  XDATCAR
[tangzeyuan@mu03 03]$ shopt -s extglob
[tangzeyuan@mu03 03]$ rm !(*.py|*.sh)
[tangzeyuan@mu03 03]$ ls
mu03.py  vasp-mu03.sh

alias

alias用来创建用户的自定义命令,通常放在~/.bashrc文件里。例如,64集群上的python版本是2.4.3,而你需要 更高版本的python来处理一些数据,这时你可以手动编译python。编译好的python放在~/opt/Python-2.7.10/目录下。 这时,在~/.bashrc文件里添加一行alias python2.7='~/opt/Python-2.7.10/bin/python'。然后,在interactive non-login shell里 输入python2.7便可以打开pytho n终端。

[tangzeyuan@hn ~]$ python2.7
Python 2.7.10 (default, Nov 20 2015, 14:02:16) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

如果,你想在shell脚本调用python2.7,而不是集群上的python2.4.3。(test.py里只有print "Hello World!"

#!/bin/bash
python2.7 test.py

你会发现找不到python2.7这个命令。这是因为在shell脚本(non-interactive)中,.bashrc是不会被读取的,除非你设置BASH_ENV

[tangzeyuan@hn temp]$ sh test.sh
test.sh: line 2: python2.7: command not found

解决方法有以下几种:

#!/bin/bash
~/opt/Python-2.7.10/bin/python test.py

这个时候,python2.7成功调用

[tangzeyuan@hn temp]$ sh test.sh
Hello world!
#!/bin/bash
mypython=`/home/tangzeyuan/opt/Python-2.7.10/bin/python`
$mypython test.py

这里需要注意的是变量里不要使用~,它并不会指向用户的home目录。变量名不要包含数字、标点,否则会出现下面的错误。

[tangzeyuan@hn temp]$ sh test.sh
test.sh: line 2: python2.7=~/opt/Python-2.7.10/bin/python: No such file or directory
test.sh: line 3: .7: command not found
#!/bin/bash
function mypython(){
    ~/opt/Python-2.7.10/bin/python "$@"
}
mypython test.py
#!/bin/bash
source ~/.bashrc
python2.7 test.py

以上4中方式最终都会成功调用自己编译的python2.7

[tangzeyuan@hn temp]$ sh test.sh
Hello world!

Read More

Sed

Head

Array/list

# Loop over temperatures variable
temperatures=(100 200 300 400 500 600 700 800 900)
temperatures=($(seq 100 100 900))
temperatures=(`seq 100 100 900`)

for t in ${temperatures[@]};
do
    echo $t
done

temperature=${temperatures[0]} # 100
temperature=${temperatures[1]} # 200
temperature=${temperatures[$SLURM_ARRAY_TASK_ID]}
echo "$temperature"K