Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7540081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:33:06+00:00 2026-05-30T07:33:06+00:00

I’m trying to create a shell script getting the process id of the Skype

  • 0

I’m trying to create a shell script getting the process id of the Skype app on my Mac.

ps -clx | grep ‘Skype’ | awk ‘{print $2}’ | head -1

The above is working fine, but there are two problems:

1)
The grep command would get all process if their name just contains “Skype”. How can I ensure that it only get the result, if the process name is exactly Skype?

2)
I would like to make a shell script from this, which can be used from the terminal but the process name should be an argument of this script:

#!/bin/sh

ps -clx | grep '$1' | awk '{print $2}' | head -1

This isn’t returning anything. I think this is because the $2 in the awk is treated as an argument too. How can I solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:33:07+00:00Added an answer on May 30, 2026 at 7:33 am

    Your ps -cl1 output looks like this:

      UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
      501   185   172      104   0  31  0  2453272   1728 -      S    ffffff80145c5ec0 ??         0:00.00 httpd
      501   303     1 80004004   0  31  0  2456440   1656 -      Ss   ffffff8015131300 ??         0:11.78 launchd
      501   307   303     4004   0  33  0  2453456   7640 -      S    ffffff8015130a80 ??         0:46.17 distnoted
      501   323   303 40004004   0  33  0  2480640   9156 -      S    ffffff80145c4dc0 ??         0:03.29 UserEventAgent
    

    Thus, the last entry in each line is your command. That means you can use the full power of regular expressions to help you.

    The $ in a regular expression means the end of the string, thus, you could use $ to specify that not only does the output must have Skype in it, it must end with Skype. This means if you have a command called Skype Controller, you won’t pull it up:

    ps -clx | grep 'Skype$' | awk '{print $2}' | head -1
    

    You can also simplify things by using the ps -o format to just pull up the columns you want:

    ps -eo pid,comm | grep 'Skype$' | awk '{print $1}' | head -1
    

    And, you can eliminate head by simply using awk‘s ability to select your line for you. In awk, NR is your record number. Thus you could do this:

    ps -eo pid,comm | grep 'Skype$' | awk 'NR == 1 {print $1}'
    

    Heck, now that I think of it, we could eliminate the grep too:

    ps -eo pid,comm | awk '/Skype$/  {print $1; exit}'
    

    This is using awk’s ability to use regular expressions. If the line contains the regular expression, ‘Skype$’, it will print the first column, then exit

    The only problem is that if you had a command Foo Skype, this will also pick it up. To eliminate that, you’ll have to do a bit more fancy footwork:

    ps -eo pid,comm | while read pid command
    do
        if [[ "$command" = "Skype" ]]
        then
            echo $pid
            break
        fi
    done
    

    The while read is reading two variables. The trick is that read uses white space to divide the variables it reads in. However, since there are only two variables, the last one will contain the rest of the entire line. Thus if the command is Skype Controller, the entire command will be put into $command even though there’s a space in it.

    Now, we don’t have to use a regular expression. We can compare the command with an equality.

    This is longer to type in, but you’re actually using fewer commands and less piping. Remember awk is looping through each line. All you’re doing here is making it more explicit. In the end, this is actually much more efficient that what you originally had.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.