2019-12-29 shell命令行操作

shell命令行操作

1.判断文件夹存在,不存在则创建,判断文件是否可以执行

查找whl文件并且复制

查找run文件并且复制

查找.sh文件并且复制

#!/bin/sh

des_file="${HOME}/file"

if [ ! -d "$des_file" ]; then

  echo "${des_file} not exists!"

  mkdir -p ${des_file}

fi

cd ${des_file} && locate whl | grep "\.whl$" | while read line; do scp ${line} . ;done

cd ${des_file} && locate run | grep "\.run$" | while read line; do scp ${line} . ;done

cd ${des_file} && locate conda | grep Linux | grep "\.sh$" | while read line; do scp ${line} . ;done

2. 下载文件

文件可执行性判断

文件添加可执行性

#!/bin/sh

filename=file.sh

rm ${filename}

wget ftp://192.168.9.5:2121/${filename}

if [ ! -x "${filename}" ]; then

  echo "${filename} not exists!"

  chmod a+x ${filename}

fi

./${filename}

3. shell管道操作

读取文件

通过管道,对每一行的内容输出

cat violence_test.txt | while read line; echo ${line}; done

4. shell中的awk命令

awk字符串操作命令

cat test.txt | awk '{print $1"===="$3}'

输出文件的第一列和第三列,中间====隔开

cat test.txt | awk '{print $1","$3}'

输出文件的第一列和第三列,中间,隔开

cat test.txt | awk '{print $1" "$3}'

输出文件的第一列和第三列,中间空格隔开

5. 合并文件

把file1.txt file2.txt 的内容合并到all.txt中

cat file1.txt file2.txt > all.txt

6. 分割字符

echo "1:3:5" | awk -F ":" '{print $NF}'

以:分割,得到最后的字符,输出结果是5

locate whl | grep "\.whl$" | awk -F "/" '{print $NF}'

输出的whl结尾的文件,而不是全路径

7. 截取字符串

https://blog.csdn.net/sayhello_world/article/details/74857480

8. 查找复制文件(version 2)

#!/bin/sh

des_file="${HOME}/file"

if [ ! -d "$des_file" ]; then

  echo "${des_file} not exists and create ${des_file}."

  mkdir -p ${des_file}

fi

cd ${des_file} && locate whl | grep "\.whl$" | while read line;

do

    if [ ! -f "${des_file}/${line##*/}" ]; then

        echo "cp ${line} ${des_file}/"

        cp ${line} ${des_file}/ ;

    fi

done

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容