Command line arguments linux shell

Linux Commands and arguments

Introduction

In this session, we have covered the shell expansion by taking a close look at commands and arguments. Knowing shell expansion is important because many commands on your Linux system are processed and most likely changed by the shell before they are executed.

The command line interface or shell used on most Linux systems is called bash, which stands for Bourne again shell. The bash shell incorporates features from sh (the original Bourne shell), csh (the C shell), and ksh (the Korn shell).

This chapter frequently uses the echo command to demonstrate shell features. The echo command is very simple: it echoes the input that it receives.

arguments

One of the primary features of a shell is to perform a command line scan. When you enter a command at the shell’s command prompt and press the enter key, then the shell will start scanning that line, cutting it up in arguments. While scanning the line, the shell may make many changes to the arguments you typed.

This process is called shell expansion. When the shell has finished scanning and modifying that line, then it will be executed.

white space removal

Parts that are separated by one or more consecutive white spaces (or tabs) are considered separate arguments, any white space is removed. The first argument is the command to be executed, the other arguments are given to the command. The shell effectively cuts your command into one or more arguments.

This explains why the following four different command lines are the same after shell expansion.

The echo command will display each argument it receives from the shell. The echo command will also add a new white space between the arguments it received.

single quotes

You can prevent the removal of white spaces by quoting the spaces. The contents of the quoted string are considered as one argument. In the screenshot below the echo receives only one argument.

double quotes

You can also prevent the removal of white spaces by double quoting the spaces. Same as above, echo only receives one argument.

Later in this tutorial, when discussing variables we will see important differences between single and double quotes.

echo and quotes

Quoted lines can include special escaped characters recognized by the echo command (when using echo -e). The screenshot below shows how to use \n for a newline and \t for a tab (usually eight white spaces).

The echo command can generate more than white spaces, tabs, and newlines. Look at the man page for a list of options.

commands

external or builtin commands ?

Not all commands are external to the shell, some are builtin. External commands are programs that have their own binary and reside somewhere in the file system. Many external commands are located in /bin or /sbin. Builtin commands are an integral part of the shell program itself.

type

To find out whether a command given to the shell will be executed as an external command or as a builtin command, use the type command.

As you can see, the cd command is builtin and the cat command is external.

You can also use this command to show you whether the command is aliased or not.

running external commands

Some commands have both builtin and external versions. When one of these commands is executed, the builtin version takes priority. To run the external version, you must enter the full path to the command.

which

The which command will search for binaries in the $PATH environment variable (variables will be explained later). In the screenshot below, it is determined that cd is builtin, and ls, cp, rm, mv, mkdir, pwd, and which are external commands.

aliases

create an alias

The shell allows you to create aliases. Aliases are often used to create an easier to remember the name for an existing command or to easily supply parameters.

abbreviate commands

An alias can also be useful to abbreviate an existing command.

Читайте также:  C windows appcompat programs amcache hve

default options

Aliases can be used to supply commands with default options. The example below shows how to set the -i option default when typing rm.

Some distributions enable default aliases to protect users from accidentally erasing files (‘rm -i’, ‘mv -i’, ‘cp -i’)

viewing aliases

You can provide one or more aliases as arguments to the alias command to get their definitions. Providing no arguments gives a complete list of current aliases.

unalias

You can undo an alias with the unalias command.

displaying shell expansion

You can display shell expansion with the set -x, and stop displaying it with set +x. You might want to use this further on in this tutorial, or when in doubt about exactly what the shell is doing with your command.

Exercise, Practice and Solution:

1. How many arguments are in this line (not counting the command itself).

touch ‘/etc/cron/cron.allow’ ‘abc123.txt’ «abc456.txt»

Code:

2. Is tac a shell builtin command ?

Code:

3. Is there an existing alias for rm ?

Code:

4. Read the man page of rm, make sure you understand the -i option of rm. Create and remove a file to test the -i option.

Code:

5. Execute: alias rm=’rm -i’ . Test your alias with a test file. Does this work as expected ?

Code:

6. List all current aliases.

Code:

7. Create an alias called ‘city’ that echoes your hometown.

Code:

8. Use your alias to test that it works.

Code:

9. Execute set -x to display shell expansion for every command.

Code:

10. Test the functionality of set +x by executing your city and rm aliases.

Code:

11. Execute set +x to stop displaying shell expansion.

Code:

12. Remove your city alias.

Code:

13. What are the location of the cat and the passwd commands?

14. Explain the difference between the following commands :
echo
/bin/echo

Code:

15. Explain the difference between the following commands :
echo Hello
echo -n Hello

Code:

16. Display A B C with two spaces between B and C.
(optional)

Code:

17. Complete the following command (do not use spaces) to display exactly as soon below

The solution is to use tabs with \t.

18. Use echo to display the following exactly:

Code:

19. Use one echo command to display three words on three lines.

Источник

Linux Shell Commands

The Shell is the command interpreter on Linux systems. This document intoduces some of the basic features of the Shell and lists many of the commands or programs available on the Linux computers in Cardiff School of Computer Science & Informatics.

The Shell

The Linux command interpreter or shell is the program users interact with in a terminal emulation window. The terminal emulation window can be one in the workstation’s Graphical User Interface mate-terminal on Linux. Alternatively, it can be an application such as SSH secure shell client or PuTTY on a Windows PC that’s logged into Linux over the network.

The shell used in the School of Computer Science & Informatics is bash Bourne Again Shell. There are other shells available such as the Bourne Shell, the C-Shell and the TC-Shell, and you can choose to use a different shell if you prefer. They all have similar characteristics but each has its own particular features. This document assumes you are using bash .

Bash has the following features:


    A command prompt which may be configured by the user. The default prompt is a dollar symbol preceded by «bash» and the bash program’s version number.

/bin containing the following.

/bin/countc
bash-2.05$ countc
45

516 ls -lrt
517 ps -ef
518 pdflatex myfile.tex

See the manual page on bash for more details (type man bash ).

Bash has an additional mechanism which allows you to recall and edit previous commands using the keyboard up-arrow key. If you press up-arrow, the last command re-appears on the terminal. Press up-arrow again to get earlier commands. To rerun the command, press RETURN . To amend the command before rerunning it, use the delete key to remove characters from the end or use the back-arrow key to reposition the cursor to delete or insert characters within the command.

Shell Commands

Here is a summary of some of the commands available. For more details refer to the manual page of each command. You can see these on-line by using the man command. Just type man followed by the name of the command you want to see.

Logging out

Command Description
logout log out of a Linux terminal

Note, on a Linux workstation you will need to exit the Desktop Environment instead.

Files and Directories

These commands allow you to create directories and handle files.

Command Description
cat concatenate and print data
lpr spool file for line printing
cd change current directory
lprm, cancel remove jobs from line printer queue
chgrp change file group
ls list and generate statistics for files
chmod change file mode
mkdir make a new directory
cp copy file data
more, page display file data at your terminal
Command Description
file determine file type
mv move or rename files
find find files
pwd print working directory
grep search file for regular expression
rm, rmdir remove (unlink) files or directories
head give first few lines
tail print last lines from file
just text justification program
touch update access and modification times of a file
lpq spool queue examination program

File Editors

Editors are used to create and amend files.

Command Description
emacs GNU project Emacs
xemacs emacs with mouse action
ex, edit line editor
Command Description
pico easy text editor for vdus
pluma Mate GUI text editor
gedit GNOME text editor
vi, vim standard text editor

Vi , pico and emacs are screen-based editors which run on a vdu or in a workstations terminal emulation window; pluma , gedit and xemacs are graphical user interface (GUI) based editors with cut and paste and mouse-controlled cursor positioning.

Manipulating data

The contents of files can be compared and altered with the following commands.

Command Description
awk pattern scanning and processing language
perl data manipulation language
cmp compare the contents of two files
paste merge file data
comm compare sorted data
sed stream text editor
cut cut out selected fields of each line of a file
sort sort file data
diff differential file comparator
Command Description
split split file into smaller files
expand, unexpand expand tabs to spaces, and vice versa
tr translate characters
gawk pattern scanning and processing language
uniq report repeated lines in a file
join join files on some common field
look find lines in sorted data
wc count words, lines, and characters

Compressed files

Files may be compressed to save space. Compressed files can be created and examined.

Command Description
gzip compress files
zmore file perusal filter for crt viewing of compressed text
uncompress uncompress files
Command Description
zcat cat a compressed file
gunzip uncompress gzipped files
zcmp, zdiff compare compressed files

Information

Manuals and documentation are available on-line. Go to our web site www.cs.cf.ac.uk/systems for web-based documentation. The following Shell commands give information.

Command Description
apropos locate commands by keyword lookup
man displays manual pages online
Command Description
info displays command information pages online
yelp GNOME help viewer

Status

These commands list or alter information about the system.

Command Description
ps print process status statistics
date print the date
quota -v display disk usage and limits
reset reset terminal mode
du print amount of disk usage
script keep script of terminal session
stty set terminal options
groups show group memberships
time time a command
homequota show quota and file usage
iostat report I/O statistics
tty print current terminal name
Command Description
kill send a signal to a process
uptime display system status
last show last logins of users
users print names of logged in users
lun list user names or login ID
vmstat report virtual memory statistics
netstat show network status
w show what logged in users are doing
who list logged in users
printenv display value of a shell variable

Printing

Files can be printed using shell commands, using the GUI print manager, or direct from some applications.

You must specify a printer by name. Printers are called

Printer Name Location
tl1_lw Teaching Lab 1 (C/2.04) laser printer
tl3_lw Teaching Lab 3 (C/2.08) laser printer
Printer Name Location
tl2_lw Teaching Lab 2 (C/2.05) laser printer
tl4_lw Teaching Lab 4 (C/2.10) laser printer

Most commands which can be used to print files, expect the printer name to be given following a -P argument.

Files may be sent to the printers as simple text files or they may be processed in various ways for the laser printers.

Command Description
lpr -P printer send a file to a printer
dvips -P printer postprocess TeX file into Postscript and print on laser printer
a2ps -P printer format text file in PostScript and print on laser printer

Messages between Users

The Linux systems support on-screen messages to other users and world-wide electronic mail.

Command Description
write send a message to another local user
wall send a message to all local users
Command Description
pine vdu-based mail utility
mail simple send or read mail program
thunderbird GUI mail handling tool on Linux

Networking

The School of Computer Science & Informatics is connected to the JANET Internet Protocol Service (JIPS), the UK Universities’ network.

These commands are used to send and receive files from Campus Linux hosts and from other hosts on JIPS and the Internet, that permit such connections, around the world.

Command Description
ftp file transfer program
tftp trivial file transfer program
sftp secure shell file transfer program
rcp remote file copy
scp secure shell remote file copy
wget non-interactive network downloader
Command Description
telnet make terminal connection to another host
ssh secure shell terminal or command connection
rlogin remote login to a Linux host
rsh remote shell
curl transfer data from a url
firefox web browser
google-chrome web browser

These commands work only where the remote host permits such connections.

Programming

The following programming tools and languages are available.

General
Command Description
make maintain groups of programs
size print program’s sizes
Command Description
nm print program’s name list
strip remove symbol table and relocation bits
Command Description
cb C program beautifier
gcc GNU ANSI C Compiler
Command Description
ctrace C program debugger
indent indent and format C program source
cxref generate C program cross reference
Command Description
g++ GNU C++ Compiler
Command Description
appletviewer JAVA applet viewer
javac JAVA compiler
eclipse Java integrated development environment on Linux
FORTRAN
Command Description
f95 GNU Fortran 95 compiler
Other Languages

(Not available on all systems).

Command Description
bc interactive arithmetic language processor
matlab maths package
gcl GNU Common Lisp
perl general purpose language
Command Description
python object-oriented programming language
squeak smalltalk
php web page embedded language
mathematica symbolic maths package
asp web page embedded language

Text Processing

TeX is a typesetting language used extensively in Linux and other operating systems for producing high-quality printed documents. Another set of programs based on Troff is the standard Linux text formatting family used, for example to format manual pages.

General Commands
Command Description
fmt simple text formatter
evince GNOME PostScript previewer
Command Description
acroread PDF viewer
spell check text for spelling error
aspell interactive spelling checker
Troff
Command Description
eqn mathematical preprocessor for troff
tbl prepare tables for nroff or troff
grap pic preprocessor for drawing graphs
troff text formatting and typesetting language
Command Description
nroff text formatting language
groff GNU troff interface for laserprinting
pic troff preprocessor for drawing pictures
Command Description
tex text formatting and typesetting
latex latex formatter
Command Description
pdflatex latex formatter with PDF output
xdvi dvi previewer
dvips convert a DVI file to POSTSCRIPT

Word Processing

LibreOffice is available on the School’s Linux systems and attempts compatibilty with Microsoft Office.

Command Description
libreoffice start LibreOffice applications

Database Management

MySQL and Oracle are available.

Command Description
sqlplus run the Oracle SQL interpreter
mysql run the mysql SQL interpreter
sqldeveloper Oracle SQL Developer GUI interface
mysql-workbench GUI interface for MySQL

Cardiff University is a member of the Russell Group of Universities
Cardiff University is a registered charity No. 1136855
Copyright © Cardiff University

Источник

Читайте также:  Linux finding command path
Оцените статью