Yacob Yacob - 1 year ago 77
Bash Question

Creating a structure (datastructure) in shell script

I'm really beginner in this field...

My question is: how can I create a structure in shell script (or bash, I'm not sure about the difference)

In C for example we can simply:

struct name
__u8 x;
__u8 y;
signed short z;
float t;
float u;

Is it possible to create something similar to this in shell?


I need to copy some values to a /proc file..but I need to be sure about they are 12 bytes, and the order of them, because I want to assign them to the previous structure which is inside a linux kernel module

Thank you for any useful answer or hint

Answer Source

May I suggest a better tool for the job, Python? It's installed on most Linux systems, and you can write scripts with it, just put the appropriate shebang at the top:

#! /usr/bin/python


#! /usr/bin/env python

Then use struct module to pack values into binary representation.

#! /usr/bin/env python
import sys, struct
# Convert first 5 arguments
args = [f(arg) for f,arg in zip([int,int,int,float,float], sys.argv[1:])]

Which is equivalent to,

#! /usr/bin/env python
import sys, struct
x = int(sys.argv[1])
y = int(sys.argv[2])
z = int(sys.argv[3])
t = float(sys.argv[4])
u = float(sys.argv[5])

And can be invoked as:

./script.py 1 2 3 4.0 5.0 > /proc/somefile...

EDIT: Or read from another file:

./script.py `cat args-file` > /proc/somefile...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download