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 /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 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·。
实例:
#!/bin/sh
ddd=`cat ./ddd.txt`
echo $ddd
d=`date`
echo $d
ddd=`cat ./ddd.txt`
echo $ddd
d=`date`
echo $d
完!