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·。

实例:

@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   / >  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·。

 实例:

# ! / bin / sh

ddd
= `cat  ./ ddd . txt`
echo   $ ddd

d
= ` date `
echo   $ d

 

 完!

 

 


 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值