WindowsBatch与LinuxShell比较[变量值来自文件或命令]
一 Windows Batch
1) 如果变量的值来自文件,则必须类似set /p num=<num.txt 。
2)如果变量的值想来自命令的结果,则一些命令可以使用%%,例如set bbb=%time%,set aaa=%date%。对于一般的命令只能先输出到临时文件,然后再读入,例如time /t > ddd.txt set /p ddd=<ddd.txt。
3) 在for中可以使用命令,例如·time /t·。
实例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
@echo
off

echo
get the value from one txt file
rem
the num.txt file only contains one line "001".
echo
work well
set
/
p num
=<
num
.
txt
echo
%num%

echo
doesn't work
set
num
=<
num
.
txt
echo
%num%
type
num
.
txt |
set
num
=
echo
%num%
type
num
.
txt |
set
/
p num
=
echo
%num%
set
num
=(
`
print
num
.
txt`
)
echo
%num%

echo
get value from
command
echo
some
command
work well
,
such as %
time
%
,
%
date
%
set
bbb
=
%
time
%
echo
%bbb%
set
aaa
=
%
date
%
echo
%aaa%

echo
general
command
doesn't work
set
ccc
=(
'
time
/
t'
)
echo
%ccc%

echo
one solution is to output the result to txt and
then
input it
time
/
t
>
ddd
.
txt
set
/
p ddd
=<
ddd
.
txt
echo
%ddd%

echo
specially
,
in
for
clause
,
(
'
time
/
t'
)
is as one
command
.
echo
and
if
also
use
setlocal
enabledelayedexpansion
,
the way also can implement get value from
command
.
setlocal
enabledelayedexpansion
for
/
f %%i in
(
'
time
/
t'
)
do
(
echo
%%i
set
ti
=
%%i
echo
!ti!
)

pause
@echo
off

echo
get the value from one txt file
rem
the num.txt file only contains one line "001".
echo
work well
set
/
p num
=<
num
.
txt
echo
%num%
echo
doesn't work
set
num
=<
num
.
txt
echo
%num%
type
num
.
txt |
set
num
=
echo
%num%
type
num
.
txt |
set
/
p num
=
echo
%num%
set
num
=(
`
print
num
.
txt`
)
echo
%num%
echo
get value from
command
echo
some
command
work well
,
such as %
time
%
,
%
date
%
set
bbb
=
%
time
%
echo
%bbb%
set
aaa
=
%
date
%
echo
%aaa%
echo
general
command
doesn't work
set
ccc
=(
'
time
/
t'
)
echo
%ccc%
echo
one solution is to output the result to txt and
then
input it
time
/
t
>
ddd
.
txt
set
/
p ddd
=<
ddd
.
txt
echo
%ddd%
echo
specially
,
in
for
clause
,
(
'
time
/
t'
)
is as one
command
.
echo
and
if
also
use
setlocal
enabledelayedexpansion
,
the way also can implement get value from
command
.
setlocal
enabledelayedexpansion
for
/
f %%i in
(
'
time
/
t'
)
do
(
echo
%%i
set
ti
=
%%i
echo
!ti!
)

pause
二 Linux Shell
1)超级简单,只需要使用·command parameters·。
实例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
#
!
/
bin
/
sh
ddd = `cat ./ ddd . txt`
echo $ ddd
d = ` date `
echo $ d
ddd = `cat ./ ddd . txt`
echo $ ddd
d = ` date `
echo $ d
完!
本文对比了Windows批处理脚本(Batch)与Linux shell脚本的功能,包括变量赋值、文件读取、命令执行等方面,并通过实例展示了两者在不同场景下的操作差异与应用技巧。

1218

被折叠的 条评论
为什么被折叠?



