作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有这个 linux 命令:
candump -l -e -x -s 0 -n 10 any,0~0,#FFFFFFFF 2>/dev/null >/tmp/can.log &
当我直接在 shell 中运行它时,它工作正常。
我想用这个方法将它添加到我的脚本中:
#!/bin/sh
# I tried this 2 syntax
# MYVAR="candump -l -e -x -s 0 -n 10 any,0~0,#FFFFFFFF 2> /dev/null > /tmp/can.log &"
MYVAR='candump -l -e -x -s 0 -n 10 any,0~0,#FFFFFFFF 2> /dev/null > /tmp/can.log &'
$MYVAR
当我执行脚本时出现此错误:
SIOCGIFINDEX:没有这样的设备
我已经测试了这些脚本并且它有效:
#!/bin/sh
MYVAR='ls -l'
$MYVAR
结果:
total 8
drwxr-xr-x 2 root root 0 Nov 5 2015 bin
drwxr-xr-x 2 root root 0 Oct 22 2015 boot
drwxr-xr-x 5 root root 13460 Jan 1 00:00 dev
drwxr-xr-x 8 root root 0 Nov 5 2015 etc
drwxr-xr-x 3 root root 0 Nov 5 2015 home
-rwsr-xr-x 1 root root 258 Nov 5 2015 init
drwxr-xr-x 3 root root 0 Nov 5 2015 lib
drwxr-xr-x 3 root root 0 Nov 5 2015 media
drwxr-xr-x 3 root root 0 Nov 5 2015 mnt
dr-xr-xr-x 67 root root 0 Jan 1 00:00 proc
drwx------ 2 root root 0 Oct 22 2015 root
drwxr-xr-x 4 root root 0 Jan 1 00:00 run
drwxr-xr-x 2 root root 0 Nov 5 2015 sbin
dr-xr-xr-x 14 root root 0 Jan 1 00:00 sys
-rwxr-xr-x 1 root root 33 Jan 1 00:04 test
drwxrwxrwt 2 root root 0 Jan 1 00:00 tmp
drwxr-xr-x 9 root root 0 Oct 22 2015 usr
drwxr-xr-x 7 root root 0 Oct 22 2015 var
我看不到错误,你能解释一下错误吗?
最佳答案
Better (and safer) to use a shell function rather that a variable :
#!/bin/sh
myfunc() {
candump -l -e -x -s 0 -n 10 any,0~0,#FFFFFFFF 2>/dev/null >/tmp/can.log &
}
# call it
myfunc
关于linux - 如何在shell变量中执行sh命令集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667973/
我是一名优秀的程序员,十分优秀!