BASH和PowerShell命令对照表

 

From: https://www.pstips.net/bash-and-powershell-quick-reference.html

ashPowerShellDescription

Scripting Basics

Put a “shebang” at the beginning of the file:
#!/bin/bash
Change permissions on script file to allow execution.
Give the file a ps1 extension. For downloaded scripts, unblock the file under file properties in Windows Explorer.Steps for making scripting files run. In PowerShell, the first time you do scripting, you will need to set the appropriate security settings: run PowerShell as administrator and type set-executionpolicy remotesigned.
source (or) ..shell built-in: execute the commands in a file
echo Stringecho String (or)
Write-Host String
Prints String to the screen. In PowerShell, Write-Host forces the output to the screen instead of being a return value.
var=0
(No spaces around =)
$var = 0Creates a variable $var. In BASH, do not put whitespace around the equals sign, and do not use a $ in the variable assignment.
let var=$var+5 (or)
var=$(( $var + 5 ))
$var += 5Add 5 to $var
commentcommentA comment

Strings

= !=-eq -ne -ceq -cneString comparisons. In BASH, be sure the strings litereals are in quotes.
"" | gm Get a list of non-static string members
[string] | gm -static Get a list of static string members
${string#text_to_remove}string.TrimStart("characters")Removes the specified characters/text from the beginning of the string.
${string%text_to_remove}string.TrimEnd("characters")Removes the specified characters/text from the end of the string.  Suppose $fnm == helloThere.txt; then ${fnm%.???} is helloThere

Pattern Matching

grepselect-stringprint lines matching a pattern
sed-replaceperforms string transformations

Booleans and Conditions

true  false$true  $falseBoolean literals
-lt -gt -le -ge -eq -ne-lt -gt -le -ge -eq -neArithmetic relational operators
 -likeTrue if a string matches a wildcard pattern
 -matchTrue if a string matches a regular expressions
 Where-Object { condition }Used to filter input by a condition. Remember that $_ refers to the current object being tested.
-z $var$var -eq $nullTrue if $var is null
-n $var$var -ne $nullTrue if $var is not null (contains one or more characters)
-o -a-or -andLogical or and and
-e fileTest-Path fileTrue if file exists.
! -e file! (Test-Path file)True if file does not exist.
-d filefile.PSISContainerTrue if file is a directory. In PowerShell, if file is not a file variable, be sure to get the file object first with gi.
-s file True if file exists and has a size greater than zero.
file1 -nt file2 True if file1 is newer (according to modification date) than file2
file1 -ot file2 True if file1 is older (according to modification date) than file2

Control Structures

if [ condition ]
then
codeblock
fi
if (condition) {
codeblock
}
If statement. In BASH, be sure to leave a space between the condition and the bracket.
if [ condition ]
then
codeblock
elif [ condition ]
then
codeblock
else
codeblock
fi
if (condition) {
codeblock
}
elseif (condition) {
codeblock
}
else {
codeblock
}
If – else if – else statement
var=0
while [ $var -lt 10 ]
do
echo $var
var=$(( $var + 1 ))
done
$var = 0
while ($var -lt 10) {
echo $var
$var++
}
Prints numbers 0 through 9.
for ((i=0; i < 10; i++)) do
echo $i
done
for ($i=0;$i -lt 10; $i++)
{
echo $i
}
Prints numbers 0 through 9.
for var in $array
do
codeblock
done
foreach ($var in $array)
{
codeblock
}
For each loop
continue  breakcontinue  breakLoop controls: continue stops current loop iteration and begins the next; break exits the loop currently being executed.
basename filefile.nameThe name of file without the path. In PowerShell, remember to first get the file object.
dirname filefile.directorynameThe name directory file is in. In PowerShell, remember to first get the file object.
stat -c%s $file (or)
$(stat -c%s $file)
file.lengthThe number of bytes in file. In PowerShell, remember to first get the file object.
 file.LastWriteTimeThe last modified time for file. Remember to first get the file object.
files=`ls` (or)
files=$(ls) (or)
files=*
$files = Get-Item *Store a list of the files in the current working directory in $files. In PowerShell, check out the -exclude flag as well as the Get-ChildItem cmdlet.
|  >  >>  2>  2>>|  >  >>  2>  2>>Piping, output and error redirection. In BASH, output redirected to /dev/null is gone. In PowerShell, output redirected to $null is gone.
printArg()
{
echo $1
}
function printArg
{
param ($p1)
echo $p1
}
function to print the first argument to the screen.
return_five()
{
return 5
}

 

return_five
echo $?

function return_five
{
echo 5
  (or)  return 5
}

 

$value = return_five
echo $value

Function returns 5, which is printed after the function call. In PowerShell, any output in a function that is not caught is returned. The return statement merely ends the function. The return value of a BASH function is stored in the variable $?.

File Information/Operations

ls Listing of files. For bash, learn the options of -lisa, -r, -R.
 lsListing of files. For PowerShell, learn -f, -r, -filter, and -exclude
treetreeGraphically displays the directory structure of a drive or path.
catcatList the contents of a file on the stdout
moremoreList the contents of a file on the stdout, pausing after each page
mkdirmkdirCreates a directory.
rmdirrmdirDeletes a folder if it is empty
pwdpwdprint working directory
cdcdChange the current directory to the one given as argument.
pushdpushdSaves the current directory name on the stack, and then cd’s the one given as argument.
popdpopdPop off the top-most name on the stack, and then cd to it
mvmvMoves or renames files. In PowerShell, check out the -force and -WhatIf flags. In BASH, check out the -f flag.
cp -rcp -rCopies files and directory trees recursively.
cpcpCopies files. In PowerShell, check out the -force and -WhatIf flags. In BASH, check out the -f flag.
rmrmDeletes a file. Check out the -r flag. In PowerShell, check out the -force and -WhatIf flags. In BASH, check out the -f flag.
catcatshow the contents of each file in sequence
moremorepagination
rmrmRemove files
ln Link (hard or soft) to an existing file.
 mklinkLink (hard or soft) to an existing file. Type cmd /c mklink to use it in PowerShell
chmodattribChange file permissions/attributes
 icaclsDisplays or modifies access control lists (ACLs) of files
chownicaclsChange ownership of a file. In PowerShell, multiple steps are necessary
umask get/set the file mode creation mask; packed vector of bits controlling the initial permissions on a newly created file
dumeasureDisk space Used. In PowerShell, try gci . -r | measure -property length -sum
wcMeasure-Objectword count, etc.
od Octal dump of file content. Almost always used with -x for hexadecimal dump
tr Translate/substitute characters; useful in improving interoperability
 assocList associations of commands with extensions. Type cmd /c assoc to use it in PowerShell
file Heuristically determine the type of file content
grepselect-stringSearch for a string in a file’s content. For now, learn it without regexp.
findgciLocate a file. By name, etc. For now, learn it without regexp.
which Gives the full path name of a command
 whereGives the full path name of a command.  Type cmd /c where to use it in PowerShell
diffdiffList the differences between two text files
cmp, diffcompare, diffshow the differences between two files
 gci . -r | sort length -descending | select -first 10get a list of the 10 largest files in the current directory (recursive)
vivimA powerful text editor. For now, learn to edit simple text files with it.
kate, leafpadnotepadSimple text editors.
emacsemacsA very powerful multi-purpose text editor. For now, learn to edit simple text files with it.

Processes

timeMeasure-Commandtimes commands, etc.
pspsshows current processes
 gps | sort ws | select -last 5Get a list of the 5 processes using the most memory
 gsv | where {$_.Status -eq "Stopped"}Get a list of stopped services
top like ps, but with continuous updates
bg place a STOPped process in the background
fg bring a backgrounded process to foreground
killkillkills a running program
ltrace show lib calls made
strace show sys calls made

System

manmanshow reference pages
setsetset the values of shell variables
setgvget and show the values of shell variables
envls env:\lists the current environment variables
$PATH$env:paththe search path
links WWW/News/Mail browser
sftp, filezilla filezillatransfer files securely to/from a remote machine
ssh, putty sshclient, puttyremote login securely
w who is on the system, and what they are doing
dfgdrshow mounted volumes, etc.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值