0

I am stuck at a problem. I want to use C variable in a shell command (dd).

Suppose abc.c is my C program.

int main()
 {
    int block = 1313; /*any integer */
    system("dd if=device of=output-file bs=4096 count=1 skip=$((block))");
    return 0;
 }

Now, if I use 1313 in place of block in the dd command then it works fine. But when I write block then it writes zeros in output-file as block is a C program variable and is used in the shell command.

1
  • 1
    You haven't googled this one, have you. And you don't know the difference between PHP and C, do you. Commented Jun 28, 2013 at 10:26

1 Answer 1

6

Use snprintf().

char buf[256];
const int block = 1313;
snprintf(buf, sizeof buf,
         "dd if=device of=output-file bs=4096 count=1 skip=%d", block);
system(buf);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.