跳至主要内容

shell vs bat

事实上, 残废的DOS批处理文件语言还是可以编写出一些比较强大的脚本来的, 虽然它们一般都需要借助于外部的工具. 所以说, 某些时候, 我们还是需要将老式的DOS批处理文件转换为UNIX shell脚本. 一般来说, 做这种事情并不困难, 因为DOS批处理文件操作不过是等价的shell脚本的一个受限子集. 


批处理文件操作符 Shell脚本等价符号 含义
% $ 命令行参数前缀
/ - 命令选项标记
\ / 目录路径分隔符
== = (等于)字符串比较测试
!==! != (不等)字符串比较测试
| | 管道
@ set +v 不打印当前命令
* * 文件名"通配符"
> > 文件重定向(覆盖)
>> >> 文件重定向(附加)
< < 重定向stdin
%VAR% $VAR 环境变量
REM # 注释
NOT ! 取反
NUL /dev/null "黑洞"用来阻止命令输出
ECHO echo 打印(Bash中有更多选项)
ECHO. echo 打印空行
ECHO OFF set +v 不打印后续的命令
FOR %%VAR IN (LIST) DO for var in
    ; do "for"循环
    :LABEL 没有等价物(多余) 标签
    GOTO 没有等价物(使用函数) 跳转到脚本的另一个位置
    PAUSE sleep 暂停或等待一段时间
    CHOICE case or select 菜单选择
    IF if if条件语句
    IF EXIST FILENAME if [ -e filename ] 测试文件是否存在
    IF !%N==! if [ -z "$N" ] 参数"N"是否存在
    CALL source命令或.(点操作符) "include"另一个脚本
    COMMAND /C source命令或.(点操作符) "include"另一个脚本(与CALL相同)
    SET export 设置一个环境变量
    SHIFT shift 左移命令行参数列表
    SGN -lt或-gt (整形)符号
    ERRORLEVEL $? 退出状态
    CON stdin "控制台"(stdin)
    PRN /dev/lp0 (一般的)打印设备
    LPT1 /dev/lp0 第一个打印设备
    COM1 /dev/ttyS0 第一个串口
    ________________________________________
    批处理文件一般都包含DOS命令. 我们必须把它转换为UNIX的等价命令, 这样我们才能把批处理文件转换为shell脚本文件.
    ________________________________________
    DOS命令 UNIX等价命令 效果
    ASSIGN ln 链接文件或目录
    ATTRIB chmod 修改文件权限
    CD cd 更换目录
    CHDIR cd 更换目录
    CLS clear 清屏
    COMP diff, comm, cmp 文件比较
    COPY cp 文件拷贝
    Ctl-C Ctl-C 中断(信号)
    Ctl-Z Ctl-D EOF(文件结束)
    DEL rm 删除文件
    DELTREE rm -rf 递归删除目录
    DIR ls -l 列出目录内容
    ERASE rm 删除文件
    EXIT exit 退出当前进程
    FC comm, cmp 文件比较
    FIND grep 在文件中查找字符串
    MD mkdir 新建目录
    MKDIR mkdir 新建目录
    MORE more 分页显示文本文件
    MOVE mv 移动文件
    PATH $PATH 可执行文件的路径
    REN mv 重命名(移动)
    RENAME mv 重命名(移动)
    RD rmdir 删除目录
    RMDIR rmdir 删除目录
    SORT sort 排序文件
    TIME date 显示系统时间
    TYPE cat 将文件输出到stdout
    XCOPY cp (扩展的)文件拷贝
    ________________________________________
    事实上, 几乎所有的UNIX和shell操作符, 还有命令都有许多的选项, 对比DOS和批处理文件来说, 它们要强大的多. 许多DOS批处理文件都需要依靠辅助工具, 比如ask.com, 这是一个比read命令差很多的类似副本. DOS对于文件名通配符扩展支持的非常有限, 并且很不完整, 仅仅识别*和?.

    摘自《高级Bash脚本编程指南》

评论

此博客中的热门博文

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.

go golang get disk usage free space remain info

package main import ( "fmt" "syscall" ) func main() { fmt.Println(DiskUsage("./")) } func DiskUsage(path string) uint64 { fs := syscall.Statfs_t{} err := syscall.Statfs(path, &fs) if err != nil { return 0 } return fs.Bfree * uint64(fs.Bsize) } //All space   = fs.Blocks * uint64(fs.Bsize) //Free space = fs.Bfree * uint64(fs.Bsize) //Used space= fs.All - disk.Free

How can I create a Windows bootable USB stick using Ubuntu?

even other Linux distros as long as  GParted  and  GRUB  are installed. Install GParted and GRUB on Ubuntu with: sudo apt-get install gparted grub-pc-bin p7zip-full ntfs-3g For BIOS: MBR partition scheme Rewrite the partition table as  msdos  and format your USB drive as  NTFS  using GParted (and then "Manage flags" and add the  boot  flag). In GParted, right click the USB partition and select  Information . Copy the UUID somewhere as you will need it. Copy all files from mounted Windows ISO or DVD to USB drive using your favorite file manager. Go to USB drive and if the folder named  boot  has uppercase characters, make them all lowercase by renaming it. Install GRUB on USB: sudo grub-install --target=i386-pc --boot-directory="/<USB_mount_folder>/boot" /dev/sdX Create a GRUB config file in the USB drive folder  boot/grub  with the name  grub.cfg . Write this into the file: echo "If...