当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

作者:精品下载站 日期:2024-12-14 04:47:20 浏览:14 分类:玩电脑

PowerShell 转义字符 | PowerShell 中的转义序列列表


[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

PowerShell 转义字符简介

在 PowerShell 中,转义字符是不属于标准字符集的特殊字符序列。它们用于指示 PowerShell 编译器如何处理序列中的下一个字符。转义字符是 PowerShell,通常以反引号 (`) 为前缀,这意味着必须以字面方式处理该字符,而不是以其他方式处理。反引号字符也称为重音符号,其 ASCII 值为 96。转义序列必须在“ ”内使用才能被视为转义序列。

语法:

以下是转义序列的示例

`t-

这用于水平制表符间距。

代码:

Write-Host "first word" `t "second word"

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

PowerShell 中可用转义序列的列表

以下是 PowerShell 中可用的一些转义序列。

Escape Sequence

使用

`0

这表示空字符

`a

这是为了警报

`b

这表示退格键

`e

这是用于转义序列的

`f

这是用于换页的

`n

这表示新行

`r

这是用于回车的

`t

这是用于水平选项卡的

`u{x}

这是 Unicode 的转义序列

`v

这表示垂直制表符空间

1.`0

这用于在 PowerShell 输出中插入空格。这与 $null 不同,$null 存储空值。这用于处理具有终止字符的文本文件。

代码:

Write-Host "Example of null character escape sequene"
Write-Host "null" `0 "character"

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

2.`一个

这用于产生蜂鸣声。这用于警告目的或向用户发送提醒。以下脚本用于产生三声蜂鸣声。

代码:

Write-host “Alert sound example”
for ($i = 0; $i -le 2; $i++)
{
"`a"
}

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

3. 退格键 (`b)

该转义序列用于将光标位置向后移动一位,但不会删除字符。

代码:

Write-Host "Example of back space escape sequene"
$input= "Vignesh Krishnakumar"
Write-Host "Actual word is "$input
Write-Host "after using back space"
Write-Host "vignesh`b`b`bkrishnakumar"

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

4. 转义符 (`e)

这用于格式化文本目的,例如修改颜色或为文本添加下划线。这些也可用于定位光标。

5.换页(`f)

这在打印文档期间使用。这将弹出当前页面并启用连续打印。

6. 新行(`n)

这用于在前一个字符之后立即插入新行。

代码:

Write-Host "new line escape sequence example"
Write-Host "first line `nsecond line `nthird line"

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

7. 回车(`r)

这用于擦除插入点之前的行。

代码:

Write-Host "carriage return escape sequence example"
Write-Host "first line second line third line "
write-host "deleting everything in above example"
Write-Host "`rprevious characters are deleted"

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

8. 水平制表符 (`t)

这用于在输出中插入制表符空格。默认情况下,PowerShell 每八个空格引入一个制表符。

代码:

Write-Host "horizontal escape sequence example"
Write-Host "first word `t second word `t vignesh `t krishnakumar"

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

9. 停止解析令牌(- -%)

当需要将输入传递给其他 cmdlet 时,将使用此序列。这可以防止 PowerShell 将字符串解释为 cmdlet 或表达式。

代码:

Write-Host "PowerShell escape sequence example"
Write-Host "escaping # character"
echo one `# two
Write-Host "new line escape sequence"
Write-Host "one `ntwo `nthree"
Write-Host "horizontal tab escape sequence"
write-Host "vignesh`tkrishnakumar`tis working`tfrom chennai `the is `t a freelancer"
Write-Host "demo of semicolon to split strings"
Write-Host "first word ; second word"
Write-Host "escaping single and double quotes"
$msg1 = 'Every "man" should pay $5000 to get "free" food'
write-Host "messages is" $msg1
$msg2="Every ""man"" should bring `$5"
write-Host "message is" $msg2

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

代码:

Write-Host "PowerShell escape sequence example"
Write-Host "escaping # character"
echo 33#44#55
Write-Host "new line escape sequence"
Write-Host "first word `second word `third word"
Write-Host "horizontal tab escape sequence"
write-Host "this`is`tis working`as dfsd `sdfdsf is `t dsa sdf"
Write-Host "demo of semicolon to split strings"
Write-Host "dan brown ; j k rolwing"
Write-Host "escaping single and double quotes"
$msg1 = 'Every "sdfdsf" sfdsf pay $33434 to sdfdsf "sdfdf" sfdsdf'
write-Host "messages is" $msg1
$msg2="sdfdsf ""sfdsf"" sfdsf sfdf `$5234324"
write-Host "message is" $msg2

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

常见错误如何处理?

代码:

write-host "Demo of single and double quotes"
write-output -inputobject 'My favorite `tteacher is 'raji''
Write-Host "The above will throw an error"
Write-Host "Correct usage is as below"
write-output -inputobject "My favorite `tteacher is 'raji'"

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

抛出错误是因为单引号包含在单引号内。通过在双引号内使用单引号可以避免这种情况。

代码:

Write-Host "Error with space example"
write-output -inputobject vignesh krishnakumar
Write-Host "Error is thrown"
Write-Host "Correct usage is as follows"
Write-Output -inputobject vignesh` Krishnakumar

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

发生错误是因为未向 PowerShell 正确指示空间。

使用正则表达式转义特殊字符

代码:

Write-Host "Escaping special characters demo"
Write-Host "Escaping backslash in path"
[regex]::Escape('C:\Vignesh\New folder')
Write-Host "Escape a string"
[regex]::Escape('Vignesh!!!Krishnakumar!!!')
[regex]::Escape('What is your age?')

输出:

[玩转系统] PowerShell 转义字符 | PowerShell 中的转义序列列表

结论

因此,本文详细介绍了 PowerShell 中的转义字符。它还详细介绍了各种可用的 PowerShell 转义序列,并通过详细示例对每个序列进行了说明。还详细解释了单引号和双引号使用过程中可能出现的各种错误以及如何处理。本文还深入介绍了如何转义特殊字符,并通过适当的示例进行了演示。它展示了如何使用正则表达式来避免特殊字符的示例。要详细了解,建议编写示例脚本并使用它们解决方法。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯