随笔 - 14  文章 - 3  评论 - 5  阅读 - 41482
  2024年11月19日

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.

1
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/

posted @ 2024-11-19 17:12 W·T·K 阅读(10) 评论(0) 推荐(0) 编辑
  2019年5月13日
摘要: 不说废话,直奔主题! 使用场景:微服务、前后端已经跨域。又不得不使用session 使用session的好处:此处省略若干字...... 做法: 客户端:该怎么写就怎么写。 服务端: Startup类 ConfigureServices 添加代码: Configure添加代码: 如果需要HttpOn 阅读全文
posted @ 2019-05-13 10:18 W·T·K 阅读(1147) 评论(2) 推荐(0) 编辑
  2018年4月2日
摘要: 1.webapi 返回cookie时,httpOnly=false 2.webapi 接收Origins 不能为* 3.js端 请求时,withCredentials必须: true //`withCredentials` indicates whether or not cross-site Ac 阅读全文
posted @ 2018-04-02 15:47 W·T·K 阅读(244) 评论(0) 推荐(0) 编辑
  2017年11月19日
摘要: 1.安装 vetur 2.在User Setting中增加设置: "vetur.format.defaultFormatter.html": "js-beautify-html" 3.搞定 格式化快捷键:Alt+Shift+F 阅读全文
posted @ 2017-11-19 10:56 W·T·K 阅读(14447) 评论(2) 推荐(5) 编辑
  2016年1月15日
摘要: 直接上代码:public string EnCryptText(string text, byte[] desKey, byte[] desIv) { MemoryStream inputStream = new MemoryStream(); ... 阅读全文
posted @ 2016-01-15 17:06 W·T·K 阅读(261) 评论(0) 推荐(0) 编辑
  2015年6月12日
摘要: 最近比较闲,不知道干点啥,想找兼职没有合适的,不找工资又不够花,o(︶︿︶)o 唉!说多了都是泪,入正题吧。首先,新建一个MVC4.0项目,建好之后打开Global.asax文件,在MVCApplication类中,只有一个方法Application_Start(),咱们就从它说起。Applicat... 阅读全文
posted @ 2015-06-12 17:07 W·T·K 阅读(1735) 评论(0) 推荐(0) 编辑
  2015年1月20日
摘要: A:return false--->In event handler ,prevents default behavior and event bubbing 。 return false 在事件的处理中,可以阻止默认事件和冒泡事件。B:event.preventDefault()---> In e... 阅读全文
posted @ 2015-01-20 13:18 W·T·K 阅读(3045) 评论(0) 推荐(0) 编辑
  2014年11月21日
摘要: 此段为原文翻译而来,原文地址WPF 中 数据绑定 ItemSource和 DataContext的不同点:1.DataContext 一般是一个非集合性质的对象,而ItemSource 更期望数据源是 集合对象。2.DataContext 是 FrameworkElement 类中定义的一个依赖属性... 阅读全文
posted @ 2014-11-21 15:51 W·T·K 阅读(9147) 评论(0) 推荐(2) 编辑
  2014年8月13日
摘要: Please open an administrative CMD window and navigate to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE and run these commands:1.deve... 阅读全文
posted @ 2014-08-13 11:36 W·T·K 阅读(695) 评论(0) 推荐(0) 编辑
  2014年3月6日
摘要: 原文地址由于工作原因,要使用ASP.NET WEBAPI(非mvc webapi),前几天时间一直很紧张,所以webapi一直将就用,今天下午好不容易有时间终于看了下,解决了自己一直疑惑的问题,在此特贴出,给大家分享。Here’s an overview of how WebAPI binds parameters to an action method. I’ll describe how parameters can be read, the set of rules that determine which technique is used, and then provide some 阅读全文
posted @ 2014-03-06 14:41 W·T·K 阅读(698) 评论(0) 推荐(0) 编辑
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示