How does Linux execute a file?
Or does linux requires some kind of standard format, like ELF format or bash format?
Yes, linux requires file to be in some supported (registered) format and execute bit set in order to execute it. Most files in Linux has either ELF format, or "shebang" format (two first symbols of them are #!
and then path to interpreter is written, used by bash, perl, python and most other scripts). Sometimes text files are allowed to execute as shell scripts, e.g. when you do ./script
from bash (handled not by kernel, but by bash shell).
More details are available in fs/exec.c file from linux kernel, beginning from do_execve
function.
There is kernel subsystem "binfmt" to register other executable formats. For example, binfmt_misc
allows you to define and register own binary format via /proc/sys/fs/binfmt_misc
special file. The execution is handled via user-defined "interpreter", the program which can read, load and execute target executable. For example, Windows PE binaries may be started with help of wine
not-an-emulator.
We can see several builtin binfmt
modules in fs
directory of kernel sources. Most common are: binfmt_elf.c
(ELF binary format) and binfmt_script.c
(which detects "shebang" and starts the interpreter). There is simple binary format "a.out" from AT&T, handled by binfmt_aout.c
, which can be easier to generate than ELF.
binfmt_aout.c 11374 bytes
binfmt_elf.c 58415 bytes
binfmt_elf_fdpic.c 48256 bytes
binfmt_em86.c 2710 bytes
binfmt_flat.c 27054 bytes
binfmt_misc.c 15175 bytes
binfmt_script.c 2768 bytes
binfmt_som.c 7315 bytes
If the file you try to execute is not of supported format, exec*
syscalls will return error:
$ hexdump -C asd
00000000 07 01 09 00 11 12 13 14 0a |.........|
00000009
$ strace ./asd
execve("./asd", ["./asd"], [/* 179 vars */]) = -1 ENOEXEC (Exec format error)
....
According to execve
man page, the return code means:
ENOEXEC
An executable is not in a recognized format, is for the wrong architecture, or has some other format error that means it cannot be executed.
How does shell execute normal program?
Man pages are helpful remainders and a nice read - not always (no... very rarely) exact and specific. You might want to read POSIX sh Command Search and Execution.
Maybe in more programming steps:
- It always spawns a child process - creates a "separate execution environment" -
fork()
. - Then it calls
exec(name_of_file)
. - (Or if for example kernel does not support shebang, the shell may parse
#!
shebang line by itself and extract executable name from it andexec
it). - If that fails, call
exec(sh, name of file)
.
What's a .sh file?
If you open your second link in a browser you'll see the source code:
#!/bin/bash
# Script to download individual .nc files from the ORNL
# Daymet server at: http://daymet.ornl.gov
[...]
# For ranges use {start..end}
# for individul vaules, use: 1 2 3 4
for year in {2002..2003}
do
for tile in {1159..1160}
do wget --limit-rate=3m http://daymet.ornl.gov/thredds/fileServer/allcf/${year}/${tile}_${year}/vp.nc -O ${tile}_${year}_vp.nc
# An example using curl instead of wget
#do curl --limit-rate 3M -o ${tile}_${year}_vp.nc http://daymet.ornl.gov/thredds/fileServer/allcf/${year}/${tile}_${year}/vp.nc
done
done
So it's a bash script. Got Linux?
In any case, the script is nothing but a series of HTTP retrievals. Both wget and curl are available for most operating systems and almost all language have HTTP libraries so it's fairly trivial to rewrite in any other technology. There're also some Windows ports of bash itself (git includes one). Last but not least, Windows 10 now has native support for Linux binaries.
Run text files in terminal
That's called a "shell script."
Add this to the top of your file:
#!/bin/sh
Then execute this command:
chmod +x filename
Then execute it like a program:
./filename
Alternately, you can execute the shell directly, telling it to execute the commands in your file:
sh -e filename
Bash Script to execute linux file
for i in `seq 0 10`
do
./bomb filename$i.txt
done
The above code will run from filename0 to filename10
Why can't the shell script file execute.Linux told me the file of no existing
Have you tries given the rights to your file with chmod +x case-7.sh
?
What interpreters do you have on your computer ? Maybe trying launching with /bin/bash case-7.sh
could solve the problem ?
If not, add the code of your .sh file so that we can help you more precisely
Related Topics
How to Pass a Complete Argument List in Bash While Keeping Mulitword Arguments Together
How to Get the Output of Ansible Ad-Hoc Command in JSON, CSV or Other Format
"Bad Interpreter" Error Message When Trying to Run Awk Executable
Perf Top Result About Nested Functions
How to Set Firefox Binary Path of Firefox in Selenium in Linux
How to Add Output "Non_Assigned" When There Is No Match in Grep
Segmentation-Fault Error Happening with Assembly Code Program
Problems with Accessing Command Line Arguments in Linux from X86 Asm
Is an Operating System Kernel an Interpeter for All Other Programs
Stop Being Root in the Middle of a Script That Was Run with Sudo
Random Alphanumeric String Linux Swift 3
Bash -Eq and ==, What's the Diff
Run Bash Script in Raspberry Pi via Ifttt
Sort Command in Not Working Properly in Unix for Sorting a CSV File
Use Grep to Find the Words That Have Two 'S' Separated by a Space