# Efficient way to combine command

**URL:** <https://community.unix.com/t/efficient-way-to-combine-command/370269>\
**Category:** Shell Programming and Scripting\
**Created:** [January 7, 2018, 10:41pm UTC](https://community.unix.com/t/efficient-way-to-combine-command/370269 "2018-01-07T22:41:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [January 7, 2018, 10:41pm UTC](https://community.unix.com/t/efficient-way-to-combine-command/370269/1 "2018-01-07T22:41:06Z")

</div>

im currently running the following command to grab all arguments in front of a script, directly from the process table.

```nohighlight
# cat /tmp/allmyprocs
ubuntu 9933 27793 0 03:29 pts/0 00:00:00 /bin/sh ./prying.sh
ubuntu 9941 9933 0 03:29 pts/0 00:00:00 sh
ubuntu 9952 9941 0 03:29 pts/0 00:00:00 sh

```

```nohighlight
myncurpid=9933
AFILENAME=prying.sh

cat /tmp/allmyprocs | awk -v JAG=9933 '$0 ~/ '${myncurpid}' / && ($2 == JAG) {print $0}' | awk -F"prying.sh" '{print substr($0,index($0,$NF))}'

which produces:

ubuntu 9933 27793 0 03:29 pts/0 00:00:00 /bin/sh ./prying.sh

```

Whenever there are no arguments passed to the "prying.sh" script, the above code outputs the entire line from the ps -ef command. I dont want that to happen. i dont want the entire line to be outputted whenever arguments are not passed.

In other words:

If there are no arguments passed to the script, print nothing.  
If there are arguments passed to the script, print only those arguments.

my command is suppose to grab everything in front of prying.sh to the end of the line. but its not doing that.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [January 8, 2018, 12:26am UTC](https://community.unix.com/t/efficient-way-to-combine-command/370269/2 "2018-01-08T00:26:48Z")

</div>

If I understand what you're trying to do, and I'm not sure that I do (since you didn't supply any sample input that would cause anything to be output), you might want to try the following:

```nohighlight
#!/bin/ksh
file=${1:-/tmp/allmyprocs}
myncurpid=9933
AFILENAME=prying.sh

awk -v JAG="$myncurpid" -v AFILENAME="$AFILENAME" '
(off = index($0, AFILENAME " ")) && $2 == JAG {
	print substr($0, off + length(AFILENAME) + 1)
}' "$file"

```

If you really want the leading space before the list of arguments passed to `prying.sh` remove the ` + 1` from the `substr()` .

One might guess that you're using an Ubuntu Linux distribution from your command prompt, but you haven't given us any indication of what shell you're using. Although written and tested using a Korn shell, the above should work with any POSIX-conforming shell. If you're using a Solaris/SunOS system, change `awk` to `/usr/xpg4/bin/awk` or `nawk` .
