0

I want to execute following command with system() function,

awk -F [][] '/dB/ { print $2 }' <(amixer sget Master) > sound_level

It gives me the desired output when I write in the terminal. But when I call this command with system function in C it gives me error.

My code is:

#include<stdio.h>
#include <stdlib.h>

int main()
{
        system("awk -F [][] '/dB/ { print $2 }' <(amixer sget Master) > sound_level");
        return 0;
}

It gives me the following error:

sh: 1: Syntax error: "(" unexpected

I have also tried:

awk -F [][] '/dB/ { print $2 }' < /(amixer sget Master /) > sound_level

but it does not work.

Any help is appreciated.

6
  • 2
    Show the code where you are trying to use this in the program. The system call Commented Nov 30, 2015 at 6:13
  • 1
    system("awk -F [][] '/dB/ { print $2 }' <(amixer sget Master) > sound_level"); Commented Nov 30, 2015 at 6:14
  • 3
    BTW: The system() function is something different than a system call. Commented Nov 30, 2015 at 6:18
  • 1
    Ulrich Eckhardt thank you for you suggestion.I have edited the question. Commented Nov 30, 2015 at 6:20
  • Concerning the code, please edit your question to include a full main() function, but stripped of really everything that is not necessary to reproduce the problem. Nothing of what you have shown here is the cause of the error you get. Commented Nov 30, 2015 at 6:22

1 Answer 1

8

Read system(3). That C function (which you probably should avoid, prefer explicit syscalls(2) like fork & execve) is running the POSIX /bin/sh -c ; read also popen(3).

But your interactive shell is probably bash which behaves differently than POSIX sh, notably for redirections like <(amixer sget Master)

To make things more complex, the /bin/bash program, when invoked as sh, changes it behavior to POSIX sh, so on many systems, /bin/sh is a symlink to /bin/bash ...

So read also the documentation of GNU bash, and Advanced Linux Programming, and Advanced Bash Scripting Guide ...

Then, either set up your pipelines and redirections with explicit syscalls(2), or code some bash or zsh script (but probably not a POSIX /bin/sh one!) to do the work.

I don't know what amixer sget Master does, but perhaps you might consider a pipeline like

 amixer sget Master | awk -F '[][]' '/dB/ { print $2 }'

(and the above pipeline is POSIX sh compatible, so callable from popen(3)...)

I am surprised you need to do all that. I guess that the sound volume could be queried by some pseudo file from /sys/

Learn more about ALSA and read some Guide thru the Linux sound API

Sign up to request clarification or add additional context in comments.

2 Comments

Don't do that. Either code some bash or zsh (but not sh !!!) shell script and run it from system, or use explicit syscalls(2)
Also worth knowing that you can start a shell with popen and feed it a script to its standard input from within your C program. That way, you don't need an extra file (that might not be available). Of course, you should only do that for very tiny scripts.

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.