OS system calls from bash script
Asked Answered
H

2

12

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 the system() command. Can some one help on this?

Headwards answered 17/4, 2012 at 17:54 Comment(1)
Why not just use the normal mechanisms for dealing with files?Marlie
N
10

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.

Nutbrown answered 17/4, 2012 at 18:1 Comment(0)
S
1

It depends on the system. For example AIX has a syscall command for this

syscall [ -n ] Name [ Argument1 ... ArgumentN ] [  ; Name [ Argument1 ... ArgumentN ] ] ...

Plan9 also has a similar command:

syscall [ -o ] entry [ arg ...  ]

On Linux there's no such command but there's mauri870/syscall which is

... an effort to port the plan9 syscall command to Linux.

For example

$ ./syscall write 1 Hello$'\n'World$'\n' 12
Hello
World
$ ./syscall -o read 0 buf 5
xyz
xyz
$ ./syscall -ov getcwd buf 100
Syscall return: 23
/home/user/src/syscall

Another port is oliwer/syscall

syscall [-<n>] name [args...] [, name [args...]]...

Example usage:

syscall open /my/file 1 0755 , write \$0 hello \#hello , close \$0
syscall open /dev/random 0 , echo \$0
Swanson answered 21/6, 2023 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.