Reputation: 21
My program requires me to do some binary conversions. The first arg[0] is the name of the file, arg[1] specifies the type of conversion, arg[2] specifies the number of bits to represent the conversion and arg[3] is the decimal or binary number depending upon the conversion. My job is to use this .txt file as a command line input so that my arg[] is each strings of the .txt file line by line . My .txt file is as follows:
#!/bin/bash
./binary -d2b -8 12
./binary -d2b -8 127
./binary -d2b -8 128
./binary -d2b -8 255
./binary -d2b -8 300
./binary -d2b -16 300
./binary -d2b -32 300
./binary -d2b -16 32767
./binary -d2b -16 32768
./binary -d2b -64 300
./binary -b2d -8 101010
./binary -b2d -8 111100001
./binary -b2d -16 111100001
./binary -b2d -16 111100001111
./binary -b2d -16 1111000011110000
./binary -b2d -32 1111000011110011
./binary -b2d -32 111100001111000011110000
./binary -b2d -32 11111111111111111111111111111111
./binary -abc -8 101
./binary -b2d -9 101
./binary -b2d -8 121
./binary -d2b -8 1a1
./binary -d2b -8
How do I make it so that each line of the .txt file is counted as argv[] arguments as in the command line input?
Upvotes: 1
Views: 534
Reputation: 11530
I realise this is obviously a homework exercise, but you seem to have missed the point, and maybe the teacher didn't explain it properly.
This text (.txt
) file is a Unix shell script. Normally we would use the .sh
extension for this sort of file. You can tell this easily by the #!
on the first line. The text after this marker tells the system the interpreter to use to parse and execute the rest of the contents of the file. In this case it's directing it to use /bin/bash
, which is often the default shell.
So... as long as this file is executable (e.g. chmod +x filename
), running it (by typing ./filename
) will execute a program called binary
with the argv[0]
set to binary
and the subsequent indexes set to the subsequent values.
If you provide a C-style executable file called binary
in the current directory it will be passed the arguments specified in the argv
parameter to its main function.
Upvotes: 4