Professional Documents
Culture Documents
overview: Unix, tcsh, AFNI the directory tree basic shell commands (class practice) running programs the shell (using the T-shell)
command line processing special characters command types shell, array and environment variables wildcards shell scripts shell commands pipes and redirection ! commands
special "iles
1
Unix
a type o" operating system (a standard), "irst developed in #$%$ e&les: !olaris,
! X, *inu&, +ree,!-
actually, *inu& and +ree,!- are not Uni& compliant, but are very similar has graphical environment, but a strength is in command-line capabilities hundreds o" commands, minimum, thousands on most systems
tcsh (T-shell)
when user types a command and hits Enter, the shell processes that command
.ust one o" the common Uni& programs (a single "ile: /bin/tcsh) other Uni& shells: sh, bash, csh, 0sh, 1sh has own synta& and sub-commands not as power"ul as bash, but more simple and readable
AFNI a suite o" data analysis tools more than 234 programs, scripts and plugins "ree and open source
2
Unix : sample commands and synta& commands: ls, cat, less, mv, cp, date, ssh, vi, rm synta&: variables ($), 5uotes (', ", `), wildcards ( , !, "#), pipes ( $ ), redirection ( % ) comments: part o" any Uni&-based system (e6g6 &'laris, (in)x, O& *) command help "rom 7man7 pages (or a boo0), e6g6 man less tcsh (T-shell) : sample commands and synta& commands: cd, set, setenv, i+, +'reach, alias, hist'r, synta&: home directories (-), history (.), .obs (/), redirecting stderr (%0) comments: single installed program, command help "rom 7man tcsh7 (or a boo0) AFNI : sample commands and synta& commands: a+ni, s)ma, 1dcalc, a+ni2pr'c3p,, 1d4ec'nv'lve synta&: sub-bric0 selection ("$#) - note: these characters appear elsewhere comments: installed suite o" programs, command help "rom -help output e6g6 a+ni2pr'c3p, -help e6g6 a+ni2pr'c3p, -help $ less
3
directories contain "iles and/or directories 5 : means either the root directory, or a directory separator
consider 5h'me5a+ni)ser5AFNI2data6
&)r+4ata5&U7A
a relative pathname depends on where you start "rom the relative pathname "or the parent directory is 7..7
many commands can be used to return to the home directory (o" 8a+ni)ser8)
cd 5h'me5a+ni)ser,
cd $8O79,
cd -,
cd -a+ni)ser
while you wor0, 0eep your location within the directory tree in mind
4
(pwd)
;6 (ow big are these programs (as "iles): tcsh, a"ni, 6cshrc, s##6proc6+T9
ls -l 3cshrc, wc s;;3pr'c3FT cd AFNI2data65FT2anal,sis,
36 <hat polynomial order is used "or the baseline in ;d-econvolve in s##6proc6+T9 !earch "or 8polort8 in the ;d-econvolve command6 (consider 0eystro0es in less:
Enter, Space, :, <, =, h, 5, n, N, >
--= down line, page, up page, go to top, bottom, help, search, ne&t, ne&t-up, 5uit) %6 <hy are the line continuation characters (7?7) use"ul9 (less s;;3pr'c3FT) --= "or readability, note: must be *'!T character on line >6 (ow many runs are processed in the +'reach r)n loops9 ("ind 8set r)ns @8) ?6 <hat are the arguments to the withinAB "unction in 1dcalc9
1dcalc -help $ less
$6 I" we run a+ni, how can we still type commands (without opening another terminal)9 (ctrl-C, :<) (also try: a+ni 0)
5
Running Programs
a program is something that gets 8e&ecuted8, or 8run8 the "irst element o" a command line is generally a program ("ollowed by a space) most shells are case sensitive when processing a command command e&les (options usually start with a 7-7) : 5:in5ls $8O795AFNI2data6 c')nt -di<its D ; ;E script: an interpreted program (interpreted by some other program) e6g6 shell script, .avascript, perl script, a"ni startup script recall: less -5AFNI2data65FT2anal,sis5s;;3pr'c3FT create a script (te&t "ile) containing a "ew commands: (<edit m,3script)
ech' Fhell' thereG ls -a c')nt H ;;
a shell is a command interpreter (case and synta& sensitive) e&les: tcsh, csh, sh, :ash, Ksh, Csh, wish, tclsh, rsh, ssh command: ech' $E the T-shell: 5:in5tcsh
evaluate special characters, such as: ~ $ & * ? decide which program to e&ecute (more on this later)
;) 2)
e&ecute appropriate program, passing to it the parameter list save the e&ecution status in the $status variable (4 is considered success) tcsh has automatic "ilename completion using the Tab 0ey
type 8ls suma8 and hit the Tab 0ey, watch what happens, and hit Enter type 8ls &'8 and hit the Tab 0ey, note what happens note: this re5uires setting the shell variable, (ilec
7
S%ecial Characters (some o" them, and some o" their uses)
- : the current user7s home directory (e6g6 5h'me5a+ni)ser), same as $8O79 $ : used to access a variable (e6g6 $path) & : used to put a command in the bac0ground (e6g6 a+ni 0) : wildcard, matching 1ero or more characters (e6g6 ech' AFNI2da ) ! : wildcard, matching e&actly one character (e6g6 ls AFNI2data!) ? : command line continuation (must be the last character on the line) ' : the shell will not evaluate most special characters contained within these 5uotes (e6g6 echo '$8O79' : will output $8O79, not 5h'me5a+ni)ser) (e6g6 1d:)cKet -pre+ix small2+)nc '+)nc2slimL'ri<"E,D33M#') " : the shell will evaluate $varia:les and `c'mmands` contained within these (e6g6 ech' "" # m, h'me dir is $8O79") (e6g6 ech' "the n)m:ers are 'c')nt H ;D'" ) ` : e&ecute the command contained within these 5uotes, and replace the 5uoted part with the output o" the contained command (e6g6 ech' "the n)m:ers are `c')nt H ;D`" )
8
Command Ty%es
the shell must decide what type o" command it has: pathname "or a program: e&ecute that program alias: apply any alias(es) then start over (decide on which program to run) shell command: part o" the 5:in5tcsh program
the 8which8 command shows where the shell gets a command "rom:
which ls which cd which wc
a list o" directories to be searched "or a given program to be run "rom the $path and $NAT8 variables are identical, but are represented di""erently commands: ech' $NAT8
ech' $path cat -53cshrc
9
Shell )aria*les
shell variables are variables that are stored in, and a""ect the shell all variables are stored as strings (or as arrays o" strings) a variable is accessed via the 7$7 character the 7ech'7 command: echo the line a"ter processing any special characters
command: ech' m, h'me dir, $8O79, h'lds -5 without arguments: 7set7 displays all variables, along with any values 7set7 ta0es a list o" variables to set, possibly with values consider the commands:
set +''d ech' $+''d set +''d @ picKle ech' $+''d set +''d eat @ ch'c'late d'n)t (emphasis: "ood eat B chocolate donut) set set +''d @ eat ch'c'late d'n)t set +''d @ "eat ch'c'late d'n)t" ech' $+''d
10
variables can be assigned the result o" a numerical computation using the 7,7 command, however only integer arithmetic is allowed commands: set val)e; @ ;H
O val)eD @ $val)e; ech' val)eD @ $val)eD D L 6
&rray )aria*les
11
-n.ironment )aria*les
similar to shell variables, but their values will propagate to children shells by convention, these variables are all upper-case (though it is not re5uired) similarly, shell variables are generally all lower-case set environment variables using 8setenv8 (as opposed to the 8set8 command) without any parameters, the 8setenv8 command will display all variables the 8setenv8 command will only set or assign one variable at a time the "ormat "or the command to set a value is (without any 7B7 sign):
setenv QARIAS(9 val)e
commands:
setenv 7T2NA79 9lvis ech' $7T2NA79 ech' $path ech' $NAT8 ech' $8O79 setenv
12
/ildcards
: matches any string o" 1ero or more characters (special case: a lone C will not match "iles starting with 767)
!
"# : matches any single character within the s5uare brac0ets "U# : matches any single character DXAD)T "or those within the brac0ets
13
Shell Scri%ts
a te&t "ile, a se5uence o" shell commands the 7 7 character can be used "or line continuation ("or readability) "or that purpose, it must be the last character on the line (including spaces) e&ecuting shell scripts, ; methods: ;B 35+ilename : (sa"est) e&ecute according to the top "J.pr'<ram8 i" no such line, usually e&ecuted via :ash (a potential error) the "ile must have e&ecute permissions (see !ls -l7, 7chm'd7) DB tcsh +ilename : e&ecute as t-shell commands 1B s')rce +ilename : e&ecute using current shell a""ects current environment this method should be used only when that is the intention (e6g6 3cshrc) recall -5AFNI2data65FT2anal,sis5s;;3pr'c3FT
create a scri%t 0te1t (ile2 called m,3script containing a (e3 commands
: change wor0ing directory : display the present wor0ing directory : set variables or assign string values to variables : set a variable to the results o" an integral computation : display or create an alias (e6g6 alias hi 'ech' hell' there' ) : put a process in the bac0ground (usually a"ter ctrl-1) : put a process in the "oreground : terminate the shell : set environment variables : e&ecute a script within the current shell environment
special 0eystro0es (to use while a process is running) ctrl-c : send an interrupt signal to the current process
ctrl-C
15
16
e6g6 tcsh -x m,3:i<3script $0 tee script3')tp)t run the script (echo commands to terminal be"ore e&ecuting) send all output to the tee program the tee program duplicates its input, sending the output to both the terminal and the given "ile (script3')tp)t) you can see the output, but it is also stored "or "uture analysis
17
Some 5S Commands
ls * cat : list the contents o" a directory : concatenate "iles to the terminal (print them to the screen)
* m're : a "ile perusal program - view "iles one page at a time * less : a better "ile perusal program (type less, get more) echo : echo command to terminal window man : on-line manuals "or many ! commands (and library "unctions) - this uses a 8less8 inter"ace to display the in"ormation - e6g6 consider man on : ls, less, man, tcsh, a+ni * head : display the top lines o" a "ile (de"ault B #4) - e6g6 6din(o (unc7slim8orig $ head 9:; * tail : display the bottom lines o" a "ile (de"ault B #4) - e6g6 tail ideal2r;3;4 * wc cp mv rm : word count - count characters, words and lines (o" a "ile) : copy "iles and directories to a new location : rename a "ile, or move "iles and direcotories : remove "iles and/or directories (,D A'ED+U* - no recovery) - e6g6 rm V)nK3+ile - e6g6 rm -r :ad3direct'r, C denotes a 7"ilter7 program, which can ta0e input "rom a "ile or "rom stdin
18
* <rep : print lines "rom a "ile that match the given pattern e6g6 <rep path -53cshrc e6g6 ls -5a:in $ <rep -i v'l e6g6 "rom the output o" 81dQ'lD&)r+ -help" show lines which contain 7sur"7, but not 7sur"ace7, then remove duplicates:
1dQ'lD&)r+ -help $ <rep s)r+ $ <rep -v s)r+ace $ s'rt $ )ni>
<s)marc : s)ma startup "ile <l'<in : commands run at the start o" a login shell (e6g6 a terminal window) <l'<')t : commands run be"ore e&iting a login shell <*ashrc : bash startup "ile (in case :ash your login shell)
19