Difference between revisions of "Tutorial: A bit of Bash"
(→Switching your Default Shell to Bash) |
|||
(17 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
__TOC__ | __TOC__ | ||
<br /> | <br /> | ||
+ | =Switching your Default Shell to Bash= | ||
<br /> | <br /> | ||
− | = | + | * Login to your class account |
+ | * At the prompt type this command: | ||
+ | |||
+ | cd | ||
+ | cp .login .login.bak | ||
+ | cp ~231a/.login .login | ||
+ | |||
+ | * The first command puts you in your home directory | ||
+ | * The second command makes a backup copy of your .login file | ||
+ | * The third command copies the .login file from the 231a account (your instructor's) over your own .login file. If you are prompted whether you want to replace your old .login file, just type '''Y''' for yes. | ||
+ | <br /> | ||
+ | * For those interested in figuring out what the new .login file contains, it simply contains 3 additional lines, at the end of the file, that, if the bash shell exists on the system, put you ''in'' the bash shell when you login to your account. | ||
+ | |||
+ | if (-x /bin/bash) then | ||
+ | exec /bin/bash -l | ||
+ | endif | ||
+ | |||
+ | <br /> | ||
+ | |||
+ | =Exploration= | ||
<br /> | <br /> | ||
A good reference on using a ''shell'' can be found here: [http://linuxcommand.org/lc3_learning_the_shell.php http://linuxcommand.org/]. | A good reference on using a ''shell'' can be found here: [http://linuxcommand.org/lc3_learning_the_shell.php http://linuxcommand.org/]. | ||
− | < | + | <P> |
Here's a recommended path through this on-line tutorial; just click on each link, read the description, and try it out on the linux server (aurora.smith.edu very likely) that you are connected to. | Here's a recommended path through this on-line tutorial; just click on each link, read the description, and try it out on the linux server (aurora.smith.edu very likely) that you are connected to. | ||
<br /> | <br /> | ||
Line 22: | Line 42: | ||
* [http://linuxcommand.org/lc3_lts0060.php Working With Commands] | * [http://linuxcommand.org/lc3_lts0060.php Working With Commands] | ||
* [http://linuxcommand.org/lc3_lts0070.php '''I/O Redirection'''] (very important) | * [http://linuxcommand.org/lc3_lts0070.php '''I/O Redirection'''] (very important) | ||
− | * [http://linuxcommand.org/lc3_lts0080.php Expansion] (stop at | + | * [http://linuxcommand.org/lc3_lts0080.php Expansion] (stop at ''arithmetic expansion'') |
* [http://linuxcommand.org/lc3_lts0090.php '''Permissions'''] (very important) | * [http://linuxcommand.org/lc3_lts0090.php '''Permissions'''] (very important) | ||
* [http://linuxcommand.org/lc3_lts0100.php (''Job Control'' --skip for later)] | * [http://linuxcommand.org/lc3_lts0100.php (''Job Control'' --skip for later)] | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 1== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | <br /> | ||
+ | * Create three files in your Linux account. | ||
+ | * The first one should be called '''file1.txt''' and reside in the path '''~/labs/lab1/'''. Its permissions should be "rwx------". It should contain the text "Mary had a little lamb." | ||
+ | * The second one should be called '''hello.asm''' and reside in '''~/hws/hw1/'''. Its permissions should be "rw-rw-rw-". It should contain the "hello world" [[CSC231_HelloWorld.asm |program]] we saw in class. | ||
+ | * The third should be the executable version of '''hello.asm''', and reside in the '''~/''' directory. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | =wc and grep= | ||
+ | <br /> | ||
+ | * Get a copy of James Joyce's Ulysses book by running this command: | ||
+ | |||
+ | wget https://www.gutenberg.org/files/4300/4300.txt | ||
+ | |||
+ | * Verify that you have a file called 4300.txt in your directory. | ||
+ | * Rename the file ulysses.txt, which is a more appropriate name. | ||
+ | * Let's view just the top 30 lines of the file: | ||
+ | |||
+ | head -30 ulysses.txt | ||
+ | |||
+ | * and the last 30 lines: | ||
+ | |||
+ | tail -30 ulysses.txt | ||
+ | |||
+ | * How about the 15th to the 20th line? Think about how you would do that... | ||
+ | |||
+ | head -20 ulysses.txt | tail -5 | ||
+ | |||
+ | * Let's get some statistics on the text file: | ||
+ | |||
+ | wc ulysses.txt | ||
+ | |||
+ | * you will observe that the file contains over 30,000 lines, over 250,000 words, and over 1.5 million characters. | ||
+ | * If you had wanted just the number of lines, you could have typed | ||
+ | |||
+ | wc -l ulysses.txt | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 2== | ||
+ | |} | ||
+ | [[Image:QuestionMark2.jpg|right|120px]] | ||
+ | <br /> | ||
+ | * Use the man pages for '''wc''' and figure out how to make it tell you the length (in character) of the longest line in the file. (To get the man page for a command, use '''man wc''' at the prompt.) | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | * Let's find all the lines in the book that contains the name of the main protagonist, '''Muliligan:" | ||
+ | |||
+ | grep Mulligan ulysses.txt | ||
+ | |||
+ | * Let's count the number of lines that contain "Mulligan": | ||
+ | |||
+ | grep Mulligan ulysses.txt | wc -l | ||
+ | |||
+ | * Let's count how many "Buck Mulligan" appear together: | ||
+ | |||
+ | grep "Buck Mulligan" ulysses.txt | wc -l | ||
+ | |||
+ | :''Note that we use double-quotes to indicate that the string "Buck Mulligan" should be treated as a block.'' | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge 3== | ||
+ | |} | ||
+ | [[Image:QuestionMark3.jpg|right|120px]] | ||
+ | * How many lines contain "Buck" '''but not''' "Mulligan"? | ||
+ | * How many lines contain "Mulligan" '''but not''' "Buck"? | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | =File System= | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge 4== | ||
+ | |} | ||
+ | [[Image:QuestionMark4.jpg|right|120px]] | ||
+ | <br /> | ||
+ | * Create 3 text files in your account. | ||
+ | * The first one should be called '''file1.txt''' and reside in the path '''~/labs/lab1/'''. Its permissions should be "rwx------". It should contain the text "Mary had a little lamb." | ||
+ | * The second one should be called '''file2.txt''', and be a copy of the previous '''file1.txt''', and it should reside in your home directory, and have privileges "rw-rw-rw-" | ||
+ | * The third one should be called '''.file3.txt''' and reside in '''~/hws/hw1/'''. Its permissions should be "rw-------". It should contain the string "I am a hidden file!" | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge 5== | ||
+ | |} | ||
+ | [[Image:QuestionMark5.jpg|right|120px]] | ||
+ | <br /> | ||
+ | * Create 3 text files in your account. | ||
+ | * The first one should be called '''file1.txt''' and reside in the path '''~/labs/lab1/'''. Its permissions should be "rwx------". It should contain the text "Mary had a little lamb." | ||
+ | * The second one should be called '''hello.asm''' and reside in '''~/hws/hw1/'''. Its permissions should be "rw-rw-rw-". It should contain the "hello world" [[CSC231_HelloWorld.asm |program]] we saw in class. | ||
+ | * The third should be the executable version of '''hello.asm''', and reside in the '''~/''' directory. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- | ||
=Creating a bash script= | =Creating a bash script= | ||
Line 146: | Line 311: | ||
for i in 10 1 10 3 3 5 6 3 2 4 7 9 6 6 6 5 ; do echo $i ; done | sort -n | uniq | for i in 10 1 10 3 3 5 6 3 2 4 7 9 6 6 6 5 ; do echo $i ; done | sort -n | uniq | ||
− | + | --> | |
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 11:57, 13 September 2015
--D. Thiebaut (talk) 11:03, 26 November 2013 (EST)
A quick review of some useful Bash commands that can be used on the command line, or that can be included in bash scripts
Contents
Switching your Default Shell to Bash
- Login to your class account
- At the prompt type this command:
cd cp .login .login.bak cp ~231a/.login .login
- The first command puts you in your home directory
- The second command makes a backup copy of your .login file
- The third command copies the .login file from the 231a account (your instructor's) over your own .login file. If you are prompted whether you want to replace your old .login file, just type Y for yes.
- For those interested in figuring out what the new .login file contains, it simply contains 3 additional lines, at the end of the file, that, if the bash shell exists on the system, put you in the bash shell when you login to your account.
if (-x /bin/bash) then exec /bin/bash -l endif
Exploration
A good reference on using a shell can be found here: http://linuxcommand.org/.
Here's a recommended path through this on-line tutorial; just click on each link, read the description, and try it out on the linux server (aurora.smith.edu very likely) that you are connected to.
- What Is "The Shell"?
- Navigation
- Looking Around
- (A Guided Tour --skip for later)
- Manipulating Files
- Working With Commands
- I/O Redirection (very important)
- Expansion (stop at arithmetic expansion)
- Permissions (very important)
- (Job Control --skip for later)
Challenge 1 |
- Create three files in your Linux account.
- The first one should be called file1.txt and reside in the path ~/labs/lab1/. Its permissions should be "rwx------". It should contain the text "Mary had a little lamb."
- The second one should be called hello.asm and reside in ~/hws/hw1/. Its permissions should be "rw-rw-rw-". It should contain the "hello world" program we saw in class.
- The third should be the executable version of hello.asm, and reside in the ~/ directory.
wc and grep
- Get a copy of James Joyce's Ulysses book by running this command:
wget https://www.gutenberg.org/files/4300/4300.txt
- Verify that you have a file called 4300.txt in your directory.
- Rename the file ulysses.txt, which is a more appropriate name.
- Let's view just the top 30 lines of the file:
head -30 ulysses.txt
- and the last 30 lines:
tail -30 ulysses.txt
- How about the 15th to the 20th line? Think about how you would do that...
head -20 ulysses.txt | tail -5
- Let's get some statistics on the text file:
wc ulysses.txt
- you will observe that the file contains over 30,000 lines, over 250,000 words, and over 1.5 million characters.
- If you had wanted just the number of lines, you could have typed
wc -l ulysses.txt
Challenge 2 |
- Use the man pages for wc and figure out how to make it tell you the length (in character) of the longest line in the file. (To get the man page for a command, use man wc at the prompt.)
- Let's find all the lines in the book that contains the name of the main protagonist, Muliligan:"
grep Mulligan ulysses.txt
- Let's count the number of lines that contain "Mulligan":
grep Mulligan ulysses.txt | wc -l
- Let's count how many "Buck Mulligan" appear together:
grep "Buck Mulligan" ulysses.txt | wc -l
- Note that we use double-quotes to indicate that the string "Buck Mulligan" should be treated as a block.
Challenge 3 |
- How many lines contain "Buck" but not "Mulligan"?
- How many lines contain "Mulligan" but not "Buck"?
File System
Challenge 4 |
- Create 3 text files in your account.
- The first one should be called file1.txt and reside in the path ~/labs/lab1/. Its permissions should be "rwx------". It should contain the text "Mary had a little lamb."
- The second one should be called file2.txt, and be a copy of the previous file1.txt, and it should reside in your home directory, and have privileges "rw-rw-rw-"
- The third one should be called .file3.txt and reside in ~/hws/hw1/. Its permissions should be "rw-------". It should contain the string "I am a hidden file!"
Challenge 5 |
- Create 3 text files in your account.
- The first one should be called file1.txt and reside in the path ~/labs/lab1/. Its permissions should be "rwx------". It should contain the text "Mary had a little lamb."
- The second one should be called hello.asm and reside in ~/hws/hw1/. Its permissions should be "rw-rw-rw-". It should contain the "hello world" program we saw in class.
- The third should be the executable version of hello.asm, and reside in the ~/ directory.