Shell脚本与常用指令

获取当前目录下所有文件名

for file in $(ls ./); do
        echo "--- ${file} ---"
done

字符串  (*注:等号两边不能有空格)

str="abcdefg"
echo "string lenth: ${#str}"
echo "First four letters [): ${str:0:4}"

数组

array=(1 2 3 4 5 6 7 8 9 0)
array[0]=9
echo "first element: ${array[0]}"
echo "array lenth: ${#array[@]}"
echo "all elements: ${array[@]}"

判断,重定向

# $1 $2 $@ $# you guess.

a=1
b=2
c=1
if (($a == $b))
then
        echo "^-^" > script.log
else
        echo "T-T" > script.log
fi


filename="qscript.sh"
if [ -e $filename ]
then
        echo "file exists" >> script.log
else
        echo "file not exists" >> script.log
fi

# > Represents redirecting the output content to the file script.log, while >> represents adding to the existing file content.
# The former overwrites the original file, and the latter does not.
# printf "%s\n %s\n"
# echo < script.log

循环,字符串拼接,eval

for ((m=1;m<=12;m++))
do
        if ((m < 10))
        then
                DDIR='/hycom/yan/data/ens_GLB/0'$m'/'
                target_a='/hycom/liu/data/ens_GLB/0'$m'/'
                target_b=${target_a}
                eval 'mkdir '${target_a}
        else
                DDIR='/hycom/yan/data/ens_GLB/'$m'/'
                target_a='/hycom/liu/data/ens_GLB/'$m'/'
                target_b=${target_a}
                eval 'mkdir '${target_a}
        fi
        fdir_num=${DDIR}'num'

        for ((i=1;i<=${m_num[m-1]};i++))
        do
                if ((i < 10))
                then
                        fdir_a=${DDIR}${fname}'0'$i${suffix_a}
                        fdir_b=${DDIR}${fname}'0'$i${suffix_b}
                else
                        fdir_a=${DDIR}${fname}$i${suffix_a}
                        fdir_b=${DDIR}${fname}$i${suffix_b}
                fi

                eval 'ln -s' ${fdir_a} ${target_a}
                eval 'ln -s' ${fdir_b} ${target_b}
        done
        target_num=${target_a}
        eval 'ln -s '${fdir_num} ${target_num}

done

 文件对比

diff analysis001_proc000.a ../dcu4_20211111/analysis001_proc000.a -y --suppress-common-lines -W 50 -H

cmp analysis001_proc000.b ../dcu4_20211111/analysis001_proc000.b -l

以这样的方式调用脚本,可以很好的将错误信息保存,帮助我们定位问题。“2>&1”,每个程序在运行后,都会至少打开三个文件描述符,分别是0:标准输入;1:标准输出;2:标准错误。

mpirun -n 800 ./EnOI_SST ${radius} ${nobs} ${alpha} >enoi.log 2>&1

常用

sinfo                                                                 查看节点信息
ln -s source dist                                                     软连接
ln source dist                                                        硬链接
ps -ef | grep slurmd                                                  查找指定进程格式
sbatch                                                                提交作业
squeue                                                                查看队列
cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l          物理CPU数量
cat /proc/cpuinfo | grep 'core id' | sort | uniq | wc -l              CUP核心数量
cat /proc/cpuinfo|grep "processor"|wc -l                              超线程
ssh computer1                                                         登录计算节点
kill -9 pid                                                           杀死进程
scontrol show node                                                    显示节点详细信息
scontrol update node=computer1 state=idle                             更改节点状态
exit logout                                                           退出节点
grep -r 11111111111                                                   查找指定字符串所在位置
free -m                                                               查看内存使用情况
:%s/str//gn                                                           在vi中统计"str"的数量

  清空缓存

echo 3 | sudo tee /proc/sys/vm/drop_caches

  hosts文件内定义的主机批量执行命令

#! /bin/bash

doCommand()
{
        hosts=`sed -n '/^[^#]/p' /hycom/liu/testForBingjie/sbatch-script/hosts`
        for host in $hosts
        do
                echo ""
                echo HOST $host
                ssh $host "$@"
        done
        return 0
}

if [ $# -lt 1 ]
then
        cmds=`sed -n '/^[^#]/p' /hycom/liu/testForBingjie/sbatch-script/command`
        doCommand "${cmds}"
        echo "return from doCommand"
        exit
fi

doCommand "$@"
echo "return from doCommand"

hosts文件<自定义>

command文件<自定义>

 

ssh免密登录配置方法

两台 Linux 主机之间配置信任关系(以及如何解除)_长安明月的博客-CSDN博客_linux信任关系配置  主机之间的 ssh 信任,简单地说,就是主机之间使用 ssh 命令登录主机不需要密码。  一、主机之间配置信任关系  将主机 A (47.100.247.242) 作为信任主机,主机 B(101.132.242.27) 作为远程主机,配置实现主机 A 免密登录到主机 B,主要步骤如下:在主机 A 中产生相应的私钥和公钥(id_rsa 和 id_rsa.pub)[testuser@cloudgw ~]$ pwd/home/testuser[testuser@cloudgw ~]$ ssh-https://blog.csdn.net/piaoranyuji/article/details/109743588

SLURM

假设您需要16个核心。这里有一些用例
您使用MPI,不关心这些核心的分布位置: --ntasks=16
您希望这些核心分布在不同的节点上: --ntasks=16 and --ntasks-per-node=1 或 --ntasks=16 and --nodes=16
您希望这些核心分布在不同的节点上,而不受其他作业的干扰: --ntasks=16 --nodes=16 --exclusive
您希望16个进程分布在8个节点上,每个节点有两个进程: --ntasks=16 --ntasks-per-node=2
您希望16个进程保持在同一节点上: --ntasks=16 --ntasks-per-node=16
您需要一个可以使用16个核心进行多线程处理的进程: --ntasks=1 --cpus-per-task=16
您需要4个进程,每个进程可以使用4个核心进行多线程处理: --ntasks=4 --cpus-per-task=4

 获取所有计算节点的同化进程所在核心序列

#! /bin/bash

hosts=`sed -n '/^[^#]/p' /hycom/liu/testForBingjie/sbatch-script/hosts`
for host in $hosts
do
        echo '********'$host'********' >> core.log

        ps=$(ssh $host "ps -ef | grep EnOI_SST")

        echo $ps >> ps.log

        pss=`sed -n '/^[^#]/p' /hycom/liu/testForBingjie/sbatch-script/ps.log`

        index=0
        pid=0
        for p in $pss
        do
                if [ "$p" == "root" ]
                then
                        let index=0
                fi

                if ((index == 1))
                then
                        let pid=$p
                fi

                if ((index == 3))
                then
                        if [ "$p" == "0" ]
                        then
                                let index=11
                                continue
                        fi
                fi
                if ((index == 7))
                then
                        if [ "$p" == "./EnOI_SST" ]
                        then
                                echo 'PID: '$pid >> core.log
                                ssh $host "taskset -pc $pid" >> core.log
                        fi
                fi
                if ((index == 10))
                then
                        let index=-1
                fi
                let index=index+1
        done
        rm ps.log
done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值