Structure, syntax of the command. Basic cmd commands - just useful

This article discusses the general syntax for shell commands ( shell) and demonstrates its use by the example of several elementary commands.

Formally command syntax defined like this:

Utility_name

Here, utility_name means name commands or utilities, options - accepted by the command the keys or optionsand operands is arguments teams. The square brackets around options and operands indicate that these options are optional and may not be present.

Let's write our "definition" in more detail:

Utility_name [-a] [-b] [-c optarg] [-d | -e] [-foptarg]

The key is a minus character followed by a single letter or number. Several keys can be combined under one minus symbol. For example, the -a and -b switches can be written as -ab. Some options may contain additional arguments (like optarg in -c and -f) that follow this option either immediately (-f) or separated from it by a space or tab character (-c). If the option accepts several arguments, they are written either separated by commas (optarg1, optarg2), or in quotes separated by spaces ("optarg1 optarg2"). Some keys can be mutually exclusive. In the example above, these are -d and -e, the pipe character indicates that these keys are mutually exclusive and only one of them should be used. If such switches are specified together on the command line, then, as a rule, the one that was encountered last will be effective. Finally, command arguments, if any, appear after all options. In the example above, the ellipsis symbol indicates that there can be multiple arguments. In this case, they are separated by spaces or tabs.

A special separator can be used to indicate the end of options -. Anything after it is accepted by the command as its argument, even if it starts with a minus character, which is otherwise interpreted as an option character. For example:

Command -abc -d optarg - -operand1 operand2

Here -operand1 acts as an argument to the command, not as an option, even though it starts with a minus sign.

When a single minus character is used as a command argument, which is a filename, it denotes either standard input or standard output, depending on the context of the command.

Most of the utilities developed by the GNU project support, in addition to "short" one-letter keys, so-called "long" options, which are one or more words separated by a hyphen. Long options are preceded by two minus symbols. The general syntax for using long options can be written like this:

Utility_name [--longopt1] [--logopt2 \u003d optarg1] [--longopt3 optarg2]

The rules for writing option arguments differ from the "short" ones in that the combined spelling of an option and its argument is not allowed, but the argument must be separated by either a space (as in --longopt3) or an "equal" symbol (as in --longopt2). These notation forms are equivalent.

In many utilities that support "long" options, as a rule, there is a one-to-one correspondence between "short" and "long" options, that is, each "short" key corresponds to one "long" option. However, the opposite statement is not always true, and the "long" option does not always have an analogue among the "short" keys.

So, with the boring theory out of the way, let's see how it all looks in practice.

The interaction of the operator with the command shell can be simplified as follows: the operator enters a command from the keyboard, the system performs some action defined by this command, and if there is any output, it displays it on the screen.

Let's analyze the following example of a dialogue between an operator and the system:

$ whoami bofh $ hostname -s lucky $ uname -sr FreeBSD 7.1-STABLE $ echo Hello, world. Hello, world.

IN this example dollar sign ($) means prompt ( prompt) of the command shell, the commands entered by the operator are in bold text, and plain text system output is shown.

In the first command (whoami), the operator tries to find out what his name is, or rather, under what user this session of work with the shell is started. As you can see, the system responded with the username bofh. Whoami does not require any parameters, it has no keys or arguments. You can try to convey them to her:

$ whoami -h whoami: illegal option - h usage: whoami $ whoami sometext usage: whoami

As you can see, the whoami utility has kindly reported that it does not know what the operator asked it for the -h switch. And the usage: whoami line suggests the command syntax.

The second command (hostname) allowed the operator to know which host the shell was running on. The operator specified the -s switch, which in this case instructs the hostname utility to display only the "short" host name, that is, without the domain part (in other words, up to the first dot).

By executing the third command (uname), the operator was able to find out which operating system he was dealing with, as well as the OS version. Keys can be written together, as shown in the example above, or separately, and then each key must be preceded by a minus symbol:

$ uname -s -r FreeBSD 7.1-STABLE

Finally, the fourth command (echo) simply prints out the arguments passed to it.

As we can see, commands can have many different keys and arguments. Their number differs from utility to utility, their meaning differs, options can be "short" and "long", etc. Moreover, in different operating systems, the same command can have a different set of keys, which can be even more confusing.

It is quite natural that it is not possible to remember all the parameters of certain commands. This is not necessary. There is a way to quickly brush up on the syntax and options of a command - using the -h and / or --help options, if the utility supports them. For example, in the example of the whoami command in FreeBSD above, it was shown that this command does not know about any keys and, moreover, is rather laconic. On GNU / Linux OS, the output looks a little different and more detailed:

$ whoami -h whoami: invalid option - "h" Try `whoami --help" for more information. $ whoami --help Usage: whoami ... Print the user name associated with the current effective user ID. Same as id -un. --help display this help and exit --version output version information and exit

As a rule, the command that supports "long" options almost always has the --help option. But, as mentioned above, this option only allows you to brush up on how to use this command and its parameters. We will talk about where to find more detailed information about a particular team in.

THEORETICAL PART

To some files from its structure, the operating system allows direct user access to perform some action or operation. The action will be performed using the programs written in these files, and the indication for such an action is called team

Internal commands operating system

The COMMAND.COM file describes the rules for a computer to execute simple, most frequently used commands. DOS commands that are written as codes in the COMMAND.COM file are called internal teams. The data from the COMMAND.COM file are loaded into memory for the entire session, so the system starts executing internal commands immediately. Here are examples of internal DOS commands, the execution codes of which are described in the COMMAND.COM file.

Team Internal command description
cd Display the name of the current directory or change directory
copy Copy a file or multiple files to a specified location
date Display the current date and change it if necessary
del, erase Delete specified files
dir List files and subdirectories in the current or specified directory
exit Terminate the work of the command processor COMMAND.COM and transfer control to the program from which it was launched
md, mdir Create directory or subdirectory
path Specify directories where DOS should look for executable files (programs)
rd, rmdir Remove directory
ren, rename Change the name of the selected file or files
time Display the system time and change it if necessary
type Display the contents of the specified file
verify Check and report the results of writing files to disk

Duplicate commands and abbreviated spellings are written separated by commas. The internal team does not require specifying who will carry it out.

External DOS Commands

In addition to DOS system files includes a large set of programs recorded in separate files with the extension .corn, .exe. A program from such a file is specially loaded into memory from disk only at the request of the command. This takes time, so the call, pointing to the filename with dOS programis called external teamDOS. External command is the name of the program that will perform the expected action; the command does not specify the file extension.

The COMMAND.COM file accepts various DOS commands from the keyboard, but itself only executes internal commands and command files sets of commands. When faced with a command that it is not ready to execute internally, the COMMAND.COM file will scan the list of directory paths and load the corresponding program file from disk for execution.

Programs for executing external commands are supplied with DOS, are stored on disk in the DOS directory as regular files, and perform maintenance actions.

Submitting a command

Team - this is an order to perform actions, operations.

1. A general-purpose command can be typed on the keyboard in Latin letters, while typing its text is displayed on the screen in the DOS prompt.

2. The command can be assigned to a keyboard key (hot key).

3. A command in programs with windowed graphic design can be described by selecting its parameters with a keyboard or mouse cursor according to the graphic display on the screen: menus, windows provided by the program.

Structure, command syntax

Above were only the names of the commands, but in most cases the command only begins with a name, and then additional elements must be written. The rules of writing and the order of the command elements are called syntax teams.

A command can contain four types of elements: command name (required at the beginning), parameters, keys (switches) and values. They determine the version of command execution: who (codes of which program, commands), over which object and how will perform the required actions. Command elements are separated by spaces.

do not archive (d)

immutable (i)

data logging (j)

safe delete (s)

top of hierarchical tree (T)

no merge at the end of the file (t)

unremovable (u)

cmp - Compares two files, and if they differ, reports the first byte and line where the difference was found.

Command syntax

The format of this command is as follows:

cmp OPTIONS ... FROM-FILE

The filename "-" stands for standard input. "Cmp" also uses standard input if one of the files is not specified.

Output status 0 means no differences were found, 1 - found, 2 - an error was found.

Prints different characters. Displays control characters with "^" and an alphabet letter, and precedes characters with the highest bit set with "-M" (representing "meta").

Ignore-initial \u003d BYTES

Ignores any differences in the first BYTES bytes of the input files. Treats files smaller than BYTES bytes as empty.

Prints all distinguishing characters. Displays control characters with a "^" character and an alphabet letter, and precedes characters with the highest bit set with the "-M" character (meaning "meta").

Quite or -s or --silent

Doesn't print anything; only returns an output status indicating whether the files are different.

Prints the offset (decimal) and value (octal) of all distinguished bytes.

V or --version

Gives the version number of cmp.

file is a Unix command for determining the type of file.

Using

file [-zL] [-f file] file ...

Keys

F file: Reads a list of files to check from the specified file.

L: Determines the type of files specified by the link.

Z: Specifies the type of files found in compressed files.

ln is a UNIX command that establishes a link between a file and a file name. Command format:

ln file1 file2

# creates a "hard" link (hard link) ln -s file1 file2

# a "symbolic" link is created

This command creates a hard link to file "file1" that will look like "file2". So for file system the original file "file1" and the link to it "file2" will actually physically be the same file. Hard links cannot point to files on other file systems.

mv (from English move) is a utility in UNIX and UNIX-like systems, used to move or rename files or directories.

Using:

If two file names are given as arguments, the name of the first file will be changed to the name of the second.

If the last argument is the name of an existing directory, then mv moves all specified files to that directory.

If the last argument is not a directory and more than two files are specified, an error message will be issued.

Keys used with mv:

–F, do not ask for confirmation of operations.

–I, prompt for confirmation of the operation when the file to be renamed or moved exists.

––, ends the list of keys. Suitable for use with files whose names begin with -.

rm (remove) is a utility on UNIX and UNIX-like systems used to remove files from the file system. Keys used with rm:

R - Process all nested subdirectories. This key required if the file to be deleted is a directory, even if it is empty. If the file to be removed is not a directory, the -r switch does not affect the rm command.

I - prompt for confirmation of each delete operation.

F - do not return an error termination code if errors were caused by non-existent files; do not ask for confirmation of operations.

rm is often synonymous with rm -i, that is, the default command prompts

confirmation before deleting files to prevent accidental deletion. If the user needs to delete a large number of files without confirming the operation, the -i switch can be canceled by adding the -f switch.

chmod - change access rights to files and folders. The name comes from the Unix OS program chmod, which actually changes the permissions for files, directories, and symbolic links.

The rights are recorded for three types of users at once: the owner of the file, the group in which he belongs and for other users. chmod can be written in two formats: numeric and character.

To understand the essence of setting rights in Unix-like systems, you need to know the representation of numbers in octal and binary number systems.

user group rest

111 101 101 rwx r-x r-x u g o

An example of the symbolic notation "rwxr-xr-x".

An example of a numeric entry is "755", which is equivalent to the above string entry: each right has a numeric code and can be set manually:

400 - the owner has the right to read;

200 - the owner has the right to write;

100 - the owner has the right to execute;

40 - the group has the right to read;

20 - the group has the right to record;

10 - the group has the right to execute;

4 - the rest have the right to read;

2 - the rest have the right to write;

1 - the rest have the right to execute.

By summing these codes, you can get a symbolic notation. For example, chmod 444 (filename): 400 + 40 + 4 \u003d 444 - everyone is read-only.

In addition to the standard "rwx" values, the CHMOD command also controls the SGID, SUID, and T bits. The set SUID or SGID attributes allow a file to be executed as the file owner or group owner, respectively.

For SUID - weight 4000, and for SGID - 2000.

Example chmod 4444 (filename) - everyone has read-only permission, but the file will be run for execution with owner rights. For folder: setting SGID will install ownership of every new generated file to the same group as the directory itself, and not to the owner's main group, as is the case by default. The SUID for the directory is meaningless.

t-bit is used with directories. When the t-bit for a directory is not set, the file in the given

NET USER - user account management

NET USER Command intended for add, edit or view user accounts on computers. When you run the command on the command line without parameters, a list of accounts is displayed windows userspresent on the computer (this command also works well on Windows 10). User account information is stored in a Windows database.

NET USER Command Syntax

net user [username [password | *] [options]]

net user username (password | *) / add [options]

net user username , where

  • username - Specifies the name of a user account that can be added, removed, edited or viewed. The name can be up to 20 characters long.
  • password - Assigns or changes the user's password. Enter an asterisk (*) to display a password prompt. Password characters are not displayed when entered from the keyboard.
  • / domain - Performs an operation on the primary domain controller for this computer.
  • net help command - Display help for the specified net command.

Additional parameters for the NET USER command

  • / active: (yes | no) -Activates or deactivates an account. If the account is not activated, the user cannot access the server. The account is activated by default.
  • / comment: "text" -Add a description for the user account (48 characters maximum). The description text is enclosed in quotation marks.
  • / countrycode: nnn -Uses the country code specified for the operating system to implement the appropriate language files when displaying custom help and error messages. A value of 0 corresponds to the default country code.
  • / expires: (date | never) -The expiration date of the account. Never is valid indefinitely. The date is in the format mm / dd / yy or dd / mm / yy depending on the country code. The month can be indicated in numbers, in full or in abbreviated form (three letters). The year can be indicated with two or four digits. Date elements are separated by a slash (/) without spaces.
  • / fullname: "name" -The user's full name (as opposed to the user's account name). The name is indicated in quotation marks.
  • / homedir: path -Specifies the path to the user's home directory. The specified location must exist.
  • / passwordchg: (yes | no) -Indicates whether the user can change his password (default can).
  • / passwordreq: (yes | no) -Indicates whether the user account should have a password (by default, it should).
  • / profilepath [: path] -Specifies the path to the user's logon profile.
  • / scriptpath: path -The path to the script used by the user to log on.
  • / times: (time | all) -Time to log in. The time parameter is specified in the format day [-day] [, day [-day]], hour [-hour] [, hour [-hour]], and the increment is 1 hour. The names of the days of the week can be indicated in full or in abbreviated form. Hours can be specified in 12 or 24 hour representation. For a 12-hour presentation, the notation is am, pm, a.m. or p.m. The value all corresponds to no time limit for logging in, and empty value denotes a complete ban on login. The day of the week and time values \u200b\u200bare comma separated; multiple entries for day of week and time values \u200b\u200bare separated by semicolons.
  • / usercomment: "text" -Allows the administrator to add or change a comment for the account.
  • / workstations: (computername [, ...] | *) -Allows you to specify up to 8 computers from which the user can log on to the network. If you do not specify a list of computers for the / workstations parameter, or you specify *, the user can log on to the network from any computer.

NET USER Command Examples

  • To display a list of all users on this computer, use the command: net user;
  • To display information about user "petr" use the following command: net user petr;
  • To add a Petr user account with a full username and the right to connect from 8 am to 5 pm, Monday through Friday, use the following command: net user petr / add / times: Mon-Fri, 08: 00-17: 00 / fullname: "Petr".
  • To delete an account, enter the command: net user petr / delete;
  • To disable account must enter the command: net user petr / active: no.

Video - Working with the NET USER utility

) is widely used as a standard notation, but people experimented in the early days. On some machines, the sort command no longer supports the + notation. However, various commands (notably ar and tar) accept controls without a prefix character - and dd (mentioned by Alok in a comment) uses a different convention altogether.

The GNU convention on using "-" for long options (supported by getopt_long (3)) has been changed to use "+". Of course, X11 software uses a single dash in front of multi-character options. So this is all a collection of historical relics where people experimented with how best to deal with it.

POSIX documents the Utility Conventions it works for, except where historical precedent is stronger.

What are the options handling styles?

[At one time SO 367309 contained the following material as my answer. It was originally asked at 2008-12-15 02:02 FerranB but has since been closed and removed.]

how many different types options do you recognize? I can think of many, including:

  • One-letter options preceded by a single dash are grouped together when there are no arguments, the argument can be attached to an option letter or in the next argument (many, many Unix commands, most POSIX commands).
  • One-letter options preceded by a single dash, grouping is not allowed, arguments must be concatenated (RCS).
  • One-letter options preceded by a single dash, grouping is not allowed, arguments must be separate (pre-POSIX SCCS, IIRC).
  • Multi-letter options preceded by a single dash can be either appended or in the next argument (X11 programs as well as Java and many Mac OS X programs with NeXTSTEP legacy).
  • Multi-letter options preceded by a single dash can be abbreviated (Atria Clearcase).
  • Multi-letter options preceded by a single plus (deprecated).
  • Multi-letter options preceded by a double dash; arguments can follow "\u003d" or be separate (GNU utilities).
  • Parameters without prefix / suffix, some names have abbreviations or implied arguments must be separated. (AmigaOS Shell)

For parameters that take an optional argument, sometimes an argument must be appended (co -p1.3 rcsfile.c), sometimes it must be followed by an "\u003d" sign. POSIX does not meaningfully support additional arguments (POSIX getopt () only allows them for the last one on the command line).

All sane option systems use the double-bar ("-") option only to indicate "end of options" - the following arguments are: "optional arguments" (usually filenames, POSIX calls them "operands") even if they begin with a dash ... Note that if a parameter is preceded by an option requiring an argument, it will be treated as an option argument and not as an "end of options" marker.)

Many, but not all, programs accept a single dash as a filename to denote standard input (usually) or standard output (sometimes). Sometimes, as with GNU "tar", both can be used on the same command line:

... | tar -cf - -F - | ...

The first personal dash means "write to stdout"; the second means "read file" names from stdin ".

Some programs use different conventions — that is, parameters that are not preceded by a dash. Many of them date back to the oldest Unix days. For example, "tar" and "ar" both accept parameters without a dash, so:

Tar cvzf /tmp/somefile.tgz some / directory

The dd command uses opt \u003d value exclusively:

Dd if \u003d / some / file of \u003d / another / file bs \u003d 16k count \u003d 200

Some programs allow you to alternate options and other arguments entirely; C compiler, make and GNU utilities work without POSIXLY_CORRECT in environment - examples. Many programs expect options that precede other arguments.

Note that git and other VCS teams often use a hybrid system:

Git commit -m "This is why it was committed"

There is a subcommand as one of the arguments. Often additional "global" parameters will be added that can be specified between the command and the subcommand. POSIX has examples; sccs command is in this category; you can argue that some of the other commands that run other commands also fall into this category: nice and xargs spring to accommodate POSIX; sudo is a non-POSIX example, as are svn and cvs.

I don't have strong preferences between different systems. when there are multiple options, then single letters with a mnemonic meaning are convenient. GNU supports this, but recommends multi-letter options, preceded by a double dash.

There are some things that I object to. One of the worst is that the same variant letter is used with different meanings depending on the fact that the other is preceded by an option letter. In my book that no, no, but I know the software where this is done.

Another undesirable behavior is inconsistency in the style of handling arguments (especially for one program, but also within a set of programs). Either requires an attached argument, or requires uncoupled arguments (or allow either), but do not have some options that require an attached argument and others that require a separate argument. And be that "\u003d" can be used to separate option and argument.

As with many, many (associated with software) things - consistency is more important than individual decisions. Using tools that automate and standardize argument handling helps with consistency.

Whatever you do, read TAOUP Command Line Options and take a look at Standards for Command Line Interfaces. (Added by J F Sebastian - thanks; I agree.)