codeSnippet

本文探讨了如何通过tftpcopyshell工具在bash中使用alias调用python脚本,传递变量,并展示如何利用shell进行多线程执行和字符串操作,涉及Python接口调用、文件管理与高级shell技巧。

tftp copy

编辑“~/.bashrc”

alias cyimg='cyimgfunc(){ \
        dstdir="/home/xxx/tftp/cusdst" ;   \
        srcdir=`date +%Y%m%d_%H%M%S.%N`"_out_put" ; \
        rm -rf ${dstdir}/"out_put";   \
        mkdir -p ${dstdir}/"out_put"; \
        if test -d "out_put"; then \
                cp -rf "out_put" ${dstdir}/${srcdir} ;  \
        else \
                echo "current No out_put" ; \
                return;                     \
        fi; \
        for itm in `ls ${dstdir}/${srcdir}` ;do \
                ln -f ${dstdir}/${srcdir}/$itm ${dstdir}/out_put/$itm; \
        done \
};cyimgfunc'

shell 调用python

传参变量

toparse.sh

#!/bin/bash

nmd=81
result=`python -c 'import doparse; print doparse.parse('${nmd}')'`

echo $result

doparse.py

#!/usr/bin/python3
#-*- coding: utf-8 -*-

def parse(var):
    print("var:", var)

shell多线程执行

#!/bin/bash

arr=(
	"www.baidu.com" \
	"www.baidu.com" \
	"www.baidu.com" \
	"www.baidu.com" \
	"www.baidu.com" \
)

start=`date +"%s"`
for element in ${arr[@]}
do
    {
		ping $element -c 5
    }&
done
wait
end=`date +"%s"`
echo "time: " `expr $end - $start`

shell字符串操作

#!/bin/bash
# 字符串拼接,直接把两个字符串放在一起实现,new=${str1}${str2}

string="https://blog.youkuaiyun.com/tianzong2019/article/details/124664734?spm=1001.2014.3001.5501"
echo "0: "${#string}  # 获取字符串长度

# ${string: start :length} 从 string 字符串的左边第 start 个字符开始,向右截取 length 个字符。
echo "1: "${string: 0: 20}
# ${string: start}	从 string 字符串的左边第 start 个字符开始截取,直到最后。
echo "2: "${string: 20}
# ${string: 0-start :length} 从 string 字符串的右边第 start 个字符开始,向右截取 length 个字符。
echo "3: "${string: 0-20: 10}
# ${string: 0-start} 从 string 字符串的右边第 start 个字符开始截取,直到最后。
echo "4: "${string: 0-20}
# ${string#*chars}	从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。
echo "5: "${string#*ti}
# ${string##*chars}	从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 右边的所有字符。
echo "6: "${string##*ti}
# ${string%chars*}	从 string 字符串第一次出现 *chars 的位置开始,截取 *chars 左边的所有字符。
echo "7: "${string%ti*}
# ${string%%chars*}	从 string 字符串最后一次出现 *chars 的位置开始,截取 *chars 左边的所有字符
echo "8: "${string%%ti*}

# output
# [root@xxx] # sh demo.sh
# 0: 84
# 1: https://blog.csdn.ne
# 2: t/tianzong2019/article/details/124664734?spm=1001.2014.3001.5501
# 3: =1001.2014
# 4: =1001.2014.3001.5501
# 5: anzong2019/article/details/124664734?spm=1001.2014.3001.5501
# 6: cle/details/124664734?spm=1001.2014.3001.5501
# 7: https://blog.youkuaiyun.com/tianzong2019/ar
# 8: https://blog.youkuaiyun.com/
### 代码片段(Code Snippet)的含义和用途 Code Snippet 是一种在集成开发环境(IDE)中使用的代码模板,旨在帮助开发者快速插入常用的代码结构。通过定义特定的快捷方式(Shortcut),开发者可以在编辑器中输入该快捷方式后按 `Tab` 键,快速生成预定义的代码块。这种机制极大地提升了编码效率,特别是在需要重复编写相似结构的代码时[^1]。 在 Visual Studio 中,Code Snippet 文件通常以 `.snippet` 为扩展名,其结构基于 XML 格式。一个典型的 Code Snippet 文件包含 `Header` 和 `Snippet` 两个主要部分。`Header` 节点中定义了代码片段的标题、快捷方式、描述、作者等元信息,而 `Snippet` 节点则包含了具体的代码模板和变量声明。例如,以下是一个用于插入 `if` 语句的代码片段定义: ```xml <?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>if</Title> <Shortcut>if</Shortcut> <Description>if 语句的代码片段</Description> <Author>Microsoft Corporation</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>expression</ID> <ToolTip>要计算的表达式</ToolTip> <Default>true</Default> </Literal> </Declarations> <Code Language="csharp"><![CDATA[if ($expression$) { $selected$ $end$ }]]></Code> </Snippet> </CodeSnippet> </CodeSnippets> ``` 在实际使用中,开发者可以输入 `if` 并按 `Tab` 键,IDE 将自动展开为一个完整的 `if` 语句结构,并允许用户通过 Tab 键在 `expression`、`selected` 等占位符之间切换,进行自定义输入[^5]。 Code Snippet 的用途不仅限于 C# 语言,在支持的 IDE 中,如 Visual Studio 和 Visual Studio Code,它同样适用于其他语言如 JavaScript、Python、Java 等。开发者可以根据需要自定义代码片段,以满足特定项目或团队的编码规范[^3]。 此外,Code Snippet 支持多种使用模式,例如 Expansion(扩展)、SurroundsWith(包围)和 Refactoring(重构)。Expansion 模式用于直接插入代码,SurroundsWith 则允许开发者选中一段代码并将其包裹在代码片段结构中,而 Refactoring 模式通常用于重构操作,如提取方法[^2]。 通过合理使用 Code Snippet,开发者可以显著减少重复性输入工作,降低语法错误率,并提高整体开发效率。无论是日常编码中常用的控制结构(如 `if`、`for`、`try-catch`),还是特定业务逻辑的模板,都可以通过代码片段的方式进行封装和复用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值