可执行文件的执行

Johnblx 2018-07-17 09:18:56
我有一个可执行文件名字为a.sh

./a.sh 可以正常启动程序

sh a.sh 或者sh ./a.sh 都得到提示:
cannot execute binary file

不理解这个是为什么呢?

我执行的环境是centos X86-64
a.sh的头信息如下:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x409880
Start of program headers: 64 (bytes into file)
Start of section headers: 5109464 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 37
Section header string table index: 34


...全文
116 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
李庆海 2018-07-17
  • 打赏
  • 举报
回复
结贴吧,有问题再讨论
Johnblx 2018-07-17
  • 打赏
  • 举报
回复
引用 5 楼 Liqinghai058 的回复:
好吧,你这样肯定不行啊,*.sh指的是一个shell脚本,而你的是一个二进制文件是先用gcc编译的,sh a是指用shell编译脚本,而./a是用gcc编译后再执行的,搞了半天你的是一个二进制文件,用sh a是肯定报错的


谢谢,大致明白了。
李庆海 2018-07-17
  • 打赏
  • 举报
回复
好吧,你这样肯定不行啊,*.sh指的是一个shell脚本,而你的是一个二进制文件是先用gcc编译的,sh a是指用shell编译脚本,而./a是用gcc编译后再执行的,搞了半天你的是一个二进制文件,用sh a是肯定报错的
Johnblx 2018-07-17
  • 打赏
  • 举报
回复
引用 3 楼 Liqinghai058 的回复:
[quote=引用 2 楼 lyyslsw 的回复:]
[quote=引用 1 楼 Liqinghai058 的回复:]
你好, ./a.sh和sh a.sh执行效果是一样的,而sh ./a.sh执行就是不对的。文件已经要是755的权限


谢谢,
不明白为什么
./a.sh 可以执行

sh a.sh 总是提示cannot execute binary file
[/quote]
脚本贴出来看下[/quote]

不是一个脚本,是一个c程序,将C程序编译好了之后生成的文件名字比如叫a,
./a 执行没有问题。
sh a 执行不了。

在同一台机器上编译运行。
用的gcc版本:

# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)


linux版本
# uname -a
Linux localhost 4.9.35-00002-g7f54cfe-dirty #57 SMP Thu Sep 21 12:45:29 CST 2017 x86_64 x86_64 x86_64 GNU/Linux
李庆海 2018-07-17
  • 打赏
  • 举报
回复
引用 2 楼 lyyslsw 的回复:
[quote=引用 1 楼 Liqinghai058 的回复:]
你好, ./a.sh和sh a.sh执行效果是一样的,而sh ./a.sh执行就是不对的。文件已经要是755的权限


谢谢,
不明白为什么
./a.sh 可以执行

sh a.sh 总是提示cannot execute binary file
[/quote]
脚本贴出来看下
Johnblx 2018-07-17
  • 打赏
  • 举报
回复
引用 1 楼 Liqinghai058 的回复:
你好, ./a.sh和sh a.sh执行效果是一样的,而sh ./a.sh执行就是不对的。文件已经要是755的权限


谢谢,
不明白为什么
./a.sh 可以执行

sh a.sh 总是提示cannot execute binary file
李庆海 2018-07-17
  • 打赏
  • 举报
回复
你好, ./a.sh和sh a.sh执行效果是一样的,而sh ./a.sh执行就是不对的。文件已经要是755的权限

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧