Skip to content

Latest commit

 

History

History
128 lines (91 loc) · 4.43 KB

c_shell_interface.md

File metadata and controls

128 lines (91 loc) · 4.43 KB

C Shell Interface

The C shell (csh) is the interface to the Unix system and is responsible for interpreting your keyboard input. When you log into a Unix system, the shell is started and runs until you log out of the system. The shell is a powerful program allowing:

  1. Connection of the output of one command as input to a second command
  2. The output of commands to be sent to files
  3. Files as input to commands
  4. Multiple command running at the same time
  5. Aliasing the names of commands
  6. Ease of running commands you have previously executed

Pipes and File Redirection

standard input, output and error

Every command has three "files" associated with it called standard input, standard output, and standard error. Input may read from standard input (keyboard), output may send to standard output (screen), and error messages get sent to standard error (logs, screen). These default devices may be changed using one of the following special shell symbols:

| | & < > >> >&

When you issue a command, the operating system creates what is called a process to execute that command. Each process is considered independently by the operating system, so it is possible to have more than one process executing at the same time.

file redirection

The C shell interprets the following symbols as the file redirection symbols:

  • < : Use file as standard input
  • > : Use file as standard output
  • >> : Append standard output to the file
  • >& : Write both standard error and standard output to the file

We can sort the contents of libraries-visits.csv and put the output into a file called sorted.csv

cd ~
sort < command_line_curriculum/data_files/libraries-visits.csv > sorted.csv
in2csv command_line_curriculum/data_files/Evidovane-knihovny.xls > converted_file.csv

pipes

A unix pipe takes the standard output of one process and makes it the standard input of another process. Use of a pipe involves:

command1 | command2

For example in the file redirection section we talked about sort and in2csv without discussing what they are. Typing sort --help and in2csv --help for most screens will not be useful. We can solve this by "piping" this to the less command:

sort --help | less
in2csv --help | less

pipes are very useful if the standard output of any command produces more lines of output than the size of your terminal. The output can be piped to the less command.

More than one pipe is allowed. To skip CSV header, get 1 column, sort names, get uniq names:

cd ~
sed '1d' command_line_curriculum/data_files/libraries-visits.csv | cut -d',' -f 1 | sort | uniq

aliasing command names

The alias command redefines the names of Unix commands. The format of the alias command is:

alias newname='command statement'

For example instead of typing ls -al && pwd which would list all the files in our current working directory we can create an alias for that with:

alias whereami='ls -al && pwd'

after that every time we type the command whereami it would display the same results of the two commands ls -al and pwd. alias is used on commands that a user uses frequently.

c shell variables

Several variables are associated exclusively with the C shell. Use the set command to assign values to these variables. The format of the set command is:

set variable = value

Let's see what our current variables look like by running the

set | less

command in our terminal. Let's change our prompt to say cliwkshp instead using the following:

history

Another variable I find useful to set is the history variable. The C shell remembers the previous commands you have executed. The history variable gives the number of previous commands to remember. We will adjust this to a larger number

set history = 2000

Let's see what commands we have run on the VM

history

reexecuting commands

There is also the ! (called the bang character) which refers to some previously executed command. Typing !! means to execute the previous command again.

Typing

!3

Exercises

  1. Get the file from ~/command_line_curriculum/names.txt and after reading the manual, sort those names alphabetically.
  2. If a user logs into a Unix system and types the following input to the shell, list the commands executed by the shell for each of the history substitutions:
w
who
ls
!1
cd /tmp
!3
!c
!w