WindowsBatch与LinuxShell比较[变量值来源于文件或命令]
WindowsBatch与LinuxShell比较[变量值来自文件或命令]
@echooff
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
echo get the value from one txt file
rem the num.txt file only contains one line "001".
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
echo work well
set/p num=<num.txt
echo %num%
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
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%
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
echo get value from command
echo some command work well, such as %time%, %date% ![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODMuZ2lm)
set bbb=%time%
echo %bbb%
set aaa=%date%
echo %aaa%
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
echo general command doesn't work
set ccc=('time/t')
echo %ccc%
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
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%
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
echo specially,in for clause,('time/t') is as one command.
echo and if also usesetlocal 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!
)
![WindowsBatch与LinuxShell比较[变量值来源于文件或命令] WindowsBatch与LinuxShell比较[变量值来源于文件或命令]](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEzLzAxLzIzLzExMjU0MTEzODIuZ2lm)
pause
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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·。
实例:
二 Linux Shell
1)超级简单,只需要使用·command parameters·。
实例:
#!/bin/sh
ddd=`cat ./ddd.txt`
echo$ddd
d=`date`
echo$d
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->ddd=`cat ./ddd.txt`
echo$ddd
d=`date`
echo$d
完!