Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS system calls from bash script

Is it possible to call os system calls like open, close etc from a shell script? I tried googling but it takes me in the wrong direction of using "system()" command. Can some one help on this?

like image 861
Harish Doddi Avatar asked Apr 17 '12 17:04

Harish Doddi


People also ask

Does shell converts the application code into system calls?

The shell is a command interpreter that provides Unix users with an interface to the underlying operating system. It interprets user commands and executes them as independent processes. The shell also allows users to code an interpretive script using simple programming constructs such as if, while and for.

What shell does OS system use?

By default it will run in the Bourne shell (that would be /bin/sh ). os.

Are shell commands System calls?

Shells directly make system calls to execute these commands, instead of forking a child process to handle them. This assignment consists of two parts.

What is $() in bash?

$() means: "first evaluate this, and then evaluate the rest of the line". Ex : echo $(pwd)/myFile.txt. will be interpreted as echo /my/path/myFile.txt. On the other hand ${} expands a variable.


1 Answers

Many syscalls are accessible, but only via the native shell mechanisms, rather than being able to directly specify exact parameters. For instance:

exec 4>outfile

calls:

open("outfile", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
dup2(3, 4)

(with 3 being replaced by the next available descriptor), and

exec 4<&-

calls:

close(4)

Some shells, such as bash, allow additional builtins to be added through loadable modules (see the enable builtin, used to load such modules); if you really needed functionality not provided upstream, you could potentially implement it that way.

like image 53
Charles Duffy Avatar answered Oct 05 '22 22:10

Charles Duffy