首页 > 系统相关 >【搬运】Linux Command Syntax – Arguments & Options Explained

【搬运】Linux Command Syntax – Arguments & Options Explained

时间:2024-11-19 17:08:09浏览次数:1  
标签:ps 00 option 31 Explained Syntax command ubuntu Command

本文摘自:https://thenerd.academy/linux-command-syntax-arguments-options-explained/

 

This lesson is all about the Linux command syntax! We’re going to explore how to customize the behavior of our commands by using arguments and options. So, fasten your seatbelts and let’s jump into the action!

Linux Command Syntax - What Are Arguments?

Remember in the previous lesson when we used the ping command to determine whether we are connected to the internet? Let’s refresh our memory by taking a look at the command we used.

ubuntu@ip-172-31-45-72:~$ ping google.com

Ping is obviously the name of our command; google.com is what we refer to as an argument. Arguments allow us to pass information and provide context to commands. In this example, our argument provides the ping command the necessary information to carry out its task.

In the Linux command syntax, arguments can be either mandatory or optional. In this case, specifying a destination is mandatory because without it, the ping command wouldn’t know which destination to communicate with.

Not all commands need arguments to function:

However, not all linux commands require arguments to function, in fact, some may not accept any arguments at all. To demonstrate, let’s take a look at a command that utilizes a different Linux command syntax and does not need any arguments to function.

Drumroll, please! Introducing the ps command, which is short for “process status.” A process refers to an active, running instance of a program within the operating system. In simpler terms, processes are applications that are currently being executed. The ps command conveniently displays all the processes on our computer. To see it in action, lets type ps in our terminal and press Enter:

ubuntu@ip-172-31-45-72:~$ ps
  PID TTY          TIME CMD
18621 pts/0    00:00:00 bash
18717 pts/0    00:00:00 ps

 

Awesome! Our computer’s processes are now on display. If you don’t recognize these processes don’t worry! We’ll delve deeper into them in a future lesson.

Notice that the ps command didn’t require any arguments to function. In fact, it doesn’t accept any arguments at all. However, that doesn’t mean we can’t customize its behavior. To do so, we need to use a different Linux command syntax, with something called “options.”

Customizing command behavior with Options:

Options are also known as “flags” or “switches,” are specific letters or words that can be typed after a command to modify its behavior in some way.

For instance, by default, the ps command doesn’t include background applications in its output. To display every process, we need to use the ps command followed by the e option where the lowercase e stands for “everything.”

ubuntu@ip-172-31-45-72:~$ ps -e

Options, like the one we’re examining here, typically start with a single hyphen. This symbol denotes that we are specifying an option and not an argument. Let’s give it a try!

ubuntu@ip-172-31-45-72:~$ ps -e
    PID TTY          TIME CMD
      1 ?        00:00:15 systemd
      2 ?        00:00:00 kthreadd
      3 ?        00:00:00 rcu_gp
      4 ?        00:00:00 rcu_par_gp
      5 ?        00:00:00 slub_flushwq
      6 ?        00:00:00 netns
      8 ?        00:00:00 kworker/0:0H-events_highpri
     10 ?        00:00:00 mm_percpu_wq
ubuntu@ip-172-31-45-72:~$

 

Awesome! The list of processes now features many more applications!

Keep in mind that different commands require different options; they’re not one-size-fits-all. For example, the e option might perform an entirely different function for another command or might not be recognized at all. In other words there is no universal Linux command syntax that works for every command.

So, how did we know that -e is a valid option for the ps command? Did we randomly type letters until something worked? Nope, we consulted something called the “manual pages,” but let’s not get ahead of ourselves here, we’ll explore these in our next lesson (here’s a sneak peek if you are curious).

Remember: Command Options are case sensitive:

When specifying options, it’s important to remember that they are case sensitive, meaning that -E with an uppercase E might not be recognized or could be misinterpreted. To demonstrate, let’s try it and see what happens.

ubuntu@ip-172-31-45-72:~$ ps -E
error: unsupported SysV option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
ubuntu@ip-172-31-45-72:~$

Uh-oh, error message! Just as we thought, the -E option with a capital E doesn’t exist within the ps command.

How to specify Options with style:

Now that we’ve got a handle on options, let’s mix things up a bit. Within a command, we can specify more than one option to further customize its behavior.

To demonstrate, let’s type our command just like before but this time let’s also include the -f option, which stands for “full format” and makes the ps command display our processes in a format that includes even more juicy details.

ubuntu@ip-172-31-45-72:~$ ps -e -f

An alternative syntax is combining all of our options under a single hyphen.

ubuntu@ip-172-31-45-72:~$ ps -ef

This Linux command syntax makes our options look neater and makes quickly identifying them easier. With our command dressed to impress, let’s hit “Enter” and watch the magic unfold.

ubuntu@ip-172-31-45-72:~$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 Feb01 ?        00:00:15 /sbin/init
root           2       0  0 Feb01 ?        00:00:00 [kthreadd]
root           3       2  0 Feb01 ?        00:00:00 [rcu_gp]
root           4       2  0 Feb01 ?        00:00:00 [rcu_par_gp]
root           5       2  0 Feb01 ?        00:00:00 [slub_flushwq]
root           6       2  0 Feb01 ?        00:00:00 [netns]
root           8       2  0 Feb01 ?        00:00:00 [kworker/0:0H-events_highpri
root          10       2  0 Feb01 ?        00:00:00 [mm_percpu_wq]
ubuntu@ip-172-31-45-72:~$

Amazing! The output is now bursting with info about each process. Don’t worry if it seems like hieroglyphics for now; remember, you’re still learning the ropes.

Get ready for a plot twist: Option-Arguments (Parameters):

We have another trick up our Linux command syntax toolkit! Options sometimes need arguments themselves! These are referred to as “option-arguments” or “parameters.”

One example of such an option is -u, which stands for “user.” This option allows us to view the processes related to a specific user. To specify a user, all we have to do is type its name after our option. In this case, this username serves as the argument for this option. Let’s see it in action!

ubuntu@ip-172-31-45-72:~$ ps -u ubuntu
    PID TTY          TIME CMD
  18536 ?        00:00:00 systemd
  18985 pts/1    00:00:00 bash
  19086 pts/1    00:00:00 ps
ubuntu@ip-172-31-45-72:~$

Ta-da! Our command now only shows the applications related to this specific user.

Here’s a fun fact: In the command we just executed, we could have typed user instead of the letter u.

ubuntu@ip-172-31-45-72:~$ ps --user ubuntu

This is what we call the “long form” of an option. Long-form options, such as this one, are usually preceded by two hyphens instead of just one. Let’s see it in action.

ubuntu@ip-172-31-45-72:~$ ps --user ubuntu
    PID TTY          TIME CMD
  18536 ?        00:00:00 systemd
  18985 pts/1    00:00:00 bash
  19087 pts/1    00:00:00 ps
ubuntu@ip-172-31-45-72:~$

As predicted, our command works like a charm. Some commands only accept the short form of an option, while others accept both.

Whether the long or short form of an option is required depends entirely on the specific command and how the developers structured its Linux command syntax. In our case, the ps command accepts both the long and the short form of this particular option.

Final Thoughts:

Wow, that was a lot! If you’re feeling a bit overwhelmed, don’t worry. Remember, it’s not about memorizing every single syntax or option right away.

As you spend more time on the command line, all this will start to feel like second nature. For now, focus on getting a feel for the linux command syntax.

Fun Facts to brighten your day:

  • Remember, options are case sensitive; ps -E is like asking a cat to bark. Not going to happen! But ps -e will work perfectly well!

  • Think of options like a command’s wardrobe. The right outfit (option) makes all the difference!

  • Remember, the command line is not just a place where you type stuff—it’s a canvas where you paint with keystrokes. Each argument is a brush stroke, and options are your palette of colors.

 

本文摘自:https://thenerd.academy/linux-command-syntax-arguments-options-explained/

标签:ps,00,option,31,Explained,Syntax,command,ubuntu,Command
From: https://www.cnblogs.com/flytigger/p/18555227

相关文章

  • WPF Datagrid event command in mvvm via behavior
    <DataGridTemplateColumnHeader="Image"><DataGridTemplateColumn.CellTemplate><DataTemplate><StackPanelOrientation="Horizontal"><CheckBoxIsThree......
  • jmeter接口测试之SSH Command
    原文地址:https://download.csdn.net/blog/column/8863406/106334136需要提前安装好的工具:按照好jmeter插件:sshprotocolsupport、sshmonsamlerCollector   然而在实际测试中,不能确定我们的的机器能够正常连接。这里就要用到while循环设置重连,如下添加while控......
  • Controller & Baseband commands速览
    目录一、设备连接与通信控制类(34条)1.1.连接参数相关1.1.1.连接建立超时设置1.1.2.链路监督超时设置1.1.3.Page操作超时设置1.1.4. 扩展Page操作超时设置1.1.5.安全连接主机支持1.2.扫描操作相关1.2.1.扫描启用与禁用1.2.2.page扫描活动设置1.2.3.查询扫描......
  • 【转载】遗传算法—HyperNEAT Explained——Advancing Neuroevolution
    原文地址:https://hunterheidenreich.com/posts/next-gen-neuroevolution-hyperneat/ExpandingNeuroEvolutionLastweek,IwroteanarticleaboutNEAT(NeuroEvolutionofAugmentingTopologies)andwediscussedalotofthecoolthingsthatsurroundedthealgori......
  • 【THM】Windows Command Line【未完成】
    task1:Introduction介绍Everyoneprefersagraphicaluserinterface(GUI)untiltheymasteracommand-lineinterface(CLI).Therearemanyreasonsforthat.OnereasonisthatGUIsareusuallyintuitive.IfsomeoneoffersyouaGUIinterfaceyouareunf......
  • "stackblitz": { "startCommand": "yarn run test:unit" } 这个命令的作用是
    在package.json文件中,stackblitz字段用于配置StackBlitz环境中的特定设置。StackBlitz是一个基于云的开发环境,允许用户在线编写、运行和调试代码。startCommand字段指定了在StackBlitz环境中启动项目时应该执行的命令。startCommand字段的作用"stackblitz":{"star......
  • Mac报错:zsh: command not found: brew
    解决方法:cd/opt /bin/bash-c"$(curl-fsSLhttps://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"or /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brewls原因目前Homebrew还没有完......
  • WPF datagrid export command in mvvm and customize delegatecommand inherited from
    publicclassDelCommand:ICommand{publiceventEventHandlerCanExecuteChanged{add{CommandManager.RequerySuggested+=value;}remove{CommandManager.RequerySuggested-=value;......
  • CommandNotFoundError: Your shell has not been properly configured to use ‘conda
    WindowsPowerShell版权所有(C)MicrosoftCorporation。保留所有权利。安装最新的PowerShell,了解新功能和改进!https://aka.ms/PSWindowsPSD:\projects\learnYolo\yolov5-5.0>condaactivatepytorchCommandNotFoundError:Yourshellhasnotbeenproperlyconfigure......
  • LE Controller commands(低功耗控制器命令)速览
    目录一、基础设置与管理命令(15个)1.1.事件控制与状态查询1.2.设备配置与特性查询1.3.性能优化与资源管理1.4.安全与隐私设置1.5.信道与特性1.6.数据长度相关二、广播与扫描(36个)2.1.广播参数设置2.2. 广播数据设置2.3.扫描响应数据设置2.4.设置随机地址2.......