Sponsored Content
Top Forums Programming Fork and Execvp Not Executing Bash Commands From C correctly. Post 302969173 by Don Cragun on Saturday 19th of March 2016 03:57:13 AM
Old 03-19-2016
First, note that you can cut a couple of processes out of your pipe line by using:
Code:
lsof 2>&1 | awk '/log/ && /openbox/ && c++ { print $10 }'

instead of:
Code:
lsof 2>&1 | grep log | awk '/openbox/ { print $10 }' | sed '1d'

Then a few comments about your C code:
  1. The fork() function returns 0 in the child (if a child is created successfully) and returns the PID of the child in the parent (if a child is created successfully), or returns -1 in the parent (if a child could not be created). Your code assumes that fork() never fails! Don't make that assumption.
  2. Operands like 2>&1, |, and grep are shell directives; not operands to be passed to the lsof utility.
  3. Instead of executing lsof, you probably need to execute sh with arguments -c and lsof 2>&1 | awk '/log/ && /openbox/ && c++ { print $10 }'.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash: executing commands and reading exit vals

I have a function that returns a bunch of exit codes, say func1, and in my code I'm trying to execute that function in an if statement. This is the closest I could get. f1call=`func1 $arg1 $arg2` if ]; then ... fi When I run the script the function never gets called. What's the right way... (7 Replies)
Discussion started by: eur0dad
7 Replies

2. Programming

after executing execvp()... program hangs up

Hi , I m actually trying to implement pipes program,but after executing the execvp(),my program is getting hanged up :mad: Actaully i m getting the desired output expected from execvp()...but once results are displayed on the output screen ,program is getting hanged up values of... (3 Replies)
Discussion started by: Crab
3 Replies

3. Shell Programming and Scripting

How to get BASH to interpret pipes in a string correctly?

In a BASH library I'm creating, I have two functions that look like: function check_process { PIDFILE=$1 if ; then PID=`cat $PIDFILE` if && ; then return 1 fi; fi; return 0 } function fork_process { CMD=$1 PIDFILE=$2 ... (2 Replies)
Discussion started by: neked
2 Replies

4. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

5. Red Hat

How commands are executing ?

Hi Folks, I have a small doubt, the binary commands under /bin and /sbin as well as other path binary files, if you peek deep into that, you can find the difference in the way of normal perl programming and some commands will be like binary files. how are the commands executing like the... (3 Replies)
Discussion started by: gsiva
3 Replies

6. Shell Programming and Scripting

Executing many commands at once

Hi, I want to run these two commands one after the other. awk 'BEGIN {OFS="\t"} {print $2}' sort -u rather than typing awk 'BEGIN {OFS="\t"} {print $2}' file1 > file2, then sort -u file2 > file3. Is it possible to run both commands on file1 then get output file3? Its kinda hard for... (5 Replies)
Discussion started by: kylle345
5 Replies

7. Programming

If I malloc my parameters, then fork and execvp, where do I free them?

Hi, I do the following: i) malloc some parameter structures ii) fork iii) in the child, I call execvp using these parameters obviously, here I can never free() the structures as execvp should not return iiii) I am currently doing waitpid in parent then freeing, but this does not seem to work... (1 Reply)
Discussion started by: misha680
1 Replies

8. Shell Programming and Scripting

OSX, bash, cat with <<MARKER executing commands

I have a script that writes another script with cat >/usr/local/bin/myscript.sh <<EOF #!/bin/sh VAR=`run a command here` EOF Problem is, after this is run, I get: $ cat /usr/local/bin/myscript.sh #!/bin/sh VAR=result of command How do I stop that from happening with Macs... (2 Replies)
Discussion started by: jnojr
2 Replies

9. UNIX for Dummies Questions & Answers

Bash does not wrap long lines correctly

Ksh is my default shell, but I want use the bash shell since its convenient to me. When I type a long command line in a terminal, it does not wrap to the next line when I reach the end of the line and it wraps onto the same line, overwriting my prompt and the rest of what I typed. $... (5 Replies)
Discussion started by: senthil.ak
5 Replies

10. Shell Programming and Scripting

Executing bash file with sudo for the second time, leads to permission denied, for some commands

I have a script that checks if the script has been ran with sudo. If the script is not ran as sudo, the current script is being executed with exec sudo bash. You are asked for a password, you type in the password, success. Everything is perfect - the commands inside the script are ran as sudo.... (1 Reply)
Discussion started by: boqsc
1 Replies
BASHBUG(1)						      General Commands Manual							BASHBUG(1)

NAME
bashbug - report a bug in bash SYNOPSIS
bashbug [--version] [--help] [email-address] DESCRIPTION
bashbug is a shell script to help the user compose and mail bug reports concerning bash in a standard format. bashbug invokes the editor specified by the environment variable EDITOR on a temporary copy of the bug report format outline. The user must fill in the appropriate fields and exit the editor. bashbug then mails the completed report to bug-bash@gnu.org, or email-address. If the report cannot be mailed, it is saved in the file dead.bashbug in the invoking user's home directory. The bug report format outline consists of several sections. The first section provides information about the machine, operating system, the bash version, and the compilation environment. The second section should be filled in with a description of the bug. The third sec- tion should be a description of how to reproduce the bug. The optional fourth section is for a proposed fix. Fixes are encouraged. ENVIRONMENT
bashbug will utilize the following environment variables if they exist: EDITOR Specifies the preferred editor. If EDITOR is not set, bashbug defaults to emacs. HOME Directory in which the failed bug report is saved if the mail fails. TMPDIR Directory in which to create temporary files and directories. SEE ALSO
bash(1) AUTHORS
Brian Fox, Free Software Foundation bfox@gnu.org Chet Ramey, Case Western Reserve University chet@po.cwru.edu GNU Bash-4.0 1998 July 30 BASHBUG(1)
All times are GMT -4. The time now is 07:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy