4. Basic Unix : light intro to shell scripts¶
What are shell scripts?
A shell script is a simple text file that contains commands in some shell language, e.g. bash, tcsh, python, matlab. Our focus is on Unix and AFNI commands using tcsh syntax (though the syntax used in this particular tutorial page could apply to either bash or tcsh).
A shell script could be typed directly into a terminal window, but is written into a text file so that it can be edited, executed multiple times, and saved as a history of processing. For these reasons, using shell scripts for data processing is highly recommended.
Essentially, all processing for published research ought to be done via shell scripts.
4.1. Inspect AFNI_data6/afni¶
Go to the AFNI_data6/afni directory and see what is there.
commands (type these in the terminal window):
cd AFNI_data6/afni pwd lsThere are anatomical, EPI, statistical and mask datasets here, along with scripts (text files of commands, such as rall_regress) and stimulus timing files (text files of times in seconds, such as stim_AV1_vis.txt).
4.2. Write a simple script¶
Create a text file that shows which directory we are in, and also shows the directory contents. Open a text editor to create a file called ‘my_script.txt’. Open it in the background, so that the prompt is returned, enabling us to type more commands in this window.
commands (type these in the terminal window):
gedit my_script.txt &If you forget to use &, either close gedit and try it again or remember to try ctrl-z and bg in the terminal window (not in the gedit window).
Type the following into the script:
echo my current directory: pwd echo "" echo the contents of this directory: lsAnd save the script.
Note
How the gedit
window looks will depend on the operating
system and environment.
4.3. Verify that the script exists¶
Use ls to verify that the my_script.txt
is now in
the current (AFNI_data6) directory.
Use ls just to verify that that
my_script.txt
is there. Do it from the terminal window that was used to run gedt. Use ls -l to get a long listing on the text file, including the size of the file and the date and time it was created.commands (type these in the terminal window):
ls ls -l my_script.txt
4.4. Display script contents¶
Display the contents of the script.
From the same command line (not in the editor), display the contents of the newly created script. Use the ‘cat’ command quickly show the contents of the file in the terminal window.
commands (type these in the terminal window):
cat my_script.txtOf course, it should look exactly like what we just typed in the editor. We did not run the script, we just displayed it as text.
4.5. Run the script¶
Execute the script from the command line.
Actually run the script now, using ‘tcsh’. This should behave as if the commands were typed into the terminal window. Think about what the output should be before running the script.
commands (type these in the terminal window):
tcsh my_script.txtThis tells tcsh to process my_script.txt as if those commands were typed at the prompt. The output should look (something) like the following:
Note
the echo command just displays the text in the terminal
the echo "" command echos a blank line (to separate output)
depending on any alias for the ls command, there may be color or special characters in the output
See also