Get Program That Satisfies the Condition

ShellShellBeginner
Practice Now

Introduction

In this project, you will learn how to create a script that can check if a program is running on a specified port and print the full path of the program or "OK" if no program is running.

๐Ÿ‘€ Preview

## Example
$ cd /home/labex/project
$ sh get.sh 3000
/usr/lib/code-server/lib/node
$ sh get.sh 43000
OK

๐ŸŽฏ Tasks

In this project, you will learn:

  • How to create a Zsh script
  • How to use the lsof command to check if a port is in use
  • How to use the ps command to get the full path of a running program

๐Ÿ† Achievements

After completing this project, you will be able to:

  • Write a script that can identify the program running on a specified port
  • Troubleshoot issues related to port conflicts in your development environment
  • Automate the process of checking for running programs on specific ports

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") linux/TextProcessingGroup -.-> linux/awk("`Text Processing`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} linux/exit -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} linux/echo -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} linux/pipeline -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} linux/sed -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} linux/awk -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} linux/cd -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} linux/ps -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/shebang -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/comments -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/quoting -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/variables_decl -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/variables_usage -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/cond_expr -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/exit_status -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/cmd_substitution -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/subshells -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} shell/globbing_expansion -.-> lab-301473{{"`Get Program That Satisfies the Condition`"}} end

Create the get.sh Script

In this step, you will create the get.sh script that will check if a program is running on a specified port.

  1. Open a text editor and create a new file named get.sh in the /home/labex/project directory.

  2. Add the following code to check if a port number is provided as an argument:

    ## Check if the port number is provided as an argument
    if [ -z "$1" ]; then
      echo "Please provide a port number."
      exit 1
    fi

    This code checks if the script was called with a port number as an argument. If not, it prints an error message and exits the script.

  3. Add the following code to get the port number:

    ## Get the port number
    port=$1

    This code stores the provided port number in the port variable.

  4. Save the get.sh file.

Check if a Program is Running on the Specified Port

In this step, you will add the code to check if a program is running on the specified port.

  1. Add the following code to the get.sh script:

    ## Check if the port is in use
    process=$(lsof -i :$port -sTCP:LISTEN -Fp | sed 's/^p//')

    This code uses the lsof command to check if a process is listening on the specified port. The output is stored in the process variable.

  2. Add the following code to check if a program is running:

    ## Check if a program is running
    if [ -z "$process" ]; then
      echo "OK"
    else
      ## Get the full path of the program
      path=$(ps -p $process -o args=)
      echo "$path" | awk '{print $1}'
    fi

    This code checks if the process variable is empty (i.e., no program is running on the specified port). If it is empty, the script prints "OK". If a program is running, the script uses the ps command to get the full path of the program and prints it.

  3. Save the get.sh file.

Test the get.sh Script

In this step, you will test the get.sh script to ensure it works as expected.

  1. Open a terminal and navigate to the /home/labex/project directory.

  2. Run the get.sh script with a port number as an argument:

    $ sh get.sh 3000
    /usr/lib/code-server/lib/node

    This should output the full path of the program running on port 3000.

  3. Run the get.sh script with a port number that is not in use:

    $ sh get.sh 43000
    OK

    This should output "OK" since no program is running on port 43000.

  4. If the script works as expected, you have completed the project.

Congratulations! You have created a script that can check if a program is running on a specified port and print the full path of the program or "OK" if no program is running.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Shell Tutorials you may like