您的位置:首页 > 运维架构 > Linux

linux使用过程中遇到的问题记录(不断更新)

2015-09-08 22:36 633 查看
1. 如何在命令行或者脚本中做简单的加减法?(2015.09.08)

答:注意要两个括号,为什么这样还不知道。


prompt> num=1; num01=$(($num+6)); echo "num01=$num01"

num01=7


2. 如何在登录服务器时自动执行一段脚本?(2015.09.16)

答:没有root权限时,将脚本放在 .bash_profile中(ubuntu 15.04)。例如,


# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
export PATH

# Check Free Cores
clear
for node in 01 02 03 04 05 06
do
total=`pbsnodes | grep -A 3 node$node | head -3 | tail -1 | awk '{print $3}'`
occ=`pbsnodes | grep -A 4 node$node | head -5 | tail -1 | grep -o "/" | wc -l `
echo -n "node$node: $(($total-$occ)) "
done
echo


3. 批处理目录下满足特定文件名规则的文件(2015.09.28)

答:用for循环(for filename in `ls`)。例如,


#!/bin/bash

scf="P0.scf.out"
nq=`grep "number of k points=" $scf | awk '{print $5}'`
echo $nq
for filename in `ls -alrt | grep "dyn" | awk '{print $9}'`
do
grep "Dynamical Matrix in cartesian axes" $filename | wc -l
done


4. awk用法之一(2015.10.05)

答: awk '{print command()}',command()可以是很多函数或运算符,比如int(),sqrt(),substr(),/ 等等。例如,


#!/bin/bash

file="/home/zhou/Desktop/MyPaper/Data/akermanite_scf.sh"
nat=`grep "nat" $file | awk -F "=" '{print $2}' | awk -F "," '{print $1}'`
grep -A $nat "ATOMIC_POSITIONS" $file | tail -n $nat | awk '{print $2," ",$3," ",$4," ",sqrt($2*$2+$3*$3+$4*$4)}'


上面的脚本求了文件中向量的模。

5. 如何在脚本中做指数运算?(2015.10.06)

答:上面用awk是一种方法,bc也是很好用的计算器。bc内置的函数有s(x)、c(x)、a(x)、l(x)、e(x)和j(n,x),分别是正弦、余弦、反正切、自然对数、自然指数和贝塞尔函数。因为xy可以写成exp(y*ln(x))的形式,所以可以用bc直接做指数运算。例如,


promote> x=2.0;y=0.5;echo -n "$x^$y= "; echo "scale=20; e($y*l($x))" | bc -l

2.0^0.5= 1.41421356237309504878

promote> x=2.0;y=0.5; echo -n "$x^$y= "; echo "$x $y" | awk '{print (($1)**($2))}'

2.0^0.5= 1.41421


6. 多核编译(2015.10.14)

答:make -j 4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: