Difference between revisions of "Tutorial: A bit of Bash"

From dftwiki3
Jump to: navigation, search
(Main Reference)
(Main Reference)
Line 9: Line 9:
 
<br />
 
<br />
 
<br />
 
<br />
=Main Reference=
+
=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/].   
<br />
+
<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 26: Line 26:
 
* [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 />
 +
<!-- ----------------------------------------------------------------------------------------------- -->
 +
{| 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 />
 
<br />
  

Revision as of 16:52, 12 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





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.




Challenge 1

QuestionMark1.jpg


  • 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.







Creating a bash script

  • first line should contain name of bash shell
#! /bin/bash
  • make the script executable
chmod +x scriptname


Variables

  • Declaration
file="hello.txt"
file=`ls -1 *.txt | head -1`     # get the first .txt file in the directory
  • In expressions: put a $ in front of it!
URL="http://cs.smith.edu/dftwiki/images/"
file="toto.png"
pathFile=$URL$file
file="alpha"
pathFile=$URL${file}.png      # add braces if string needs to be concatenated to variable

Script Parameters

  • assume doit.sh is a script and it is called as follows:
 doit.sh 8 2000 50
  • To get the arguments inside the script:
NP=$1    # will get 8
N=$2     # will get 2000
M=$3     # will get 50
  • To test if the number of parameters is 3 inside the script:
if [ "$#" -ne 3 ]; then
    echo "syntax: doit.sh NP N M"
    exit
fi

Displaying strings and various quantities

Getting the length of a string variable

name="toto.txt"
echo "length of $name = ${#name}"      #will output 8

Getting extension and name (without extension) of a file

fullPath="/usr/local/bin/toto.txt"
fileName="${fullPath##*/}"
echo "file name with extension = $fileName"

extension="${fullPath##*.}"
fileName="${fileName%.*}"

echo "fullPath = $fullPath"
echo "fileName without extension = $fileName"
echo "extension = $extension"

For Loops

  • loop from 1 to 10
for i in 1 2 3 4 5 6 7 8 9 10 ; do 
    echo $i
done
or
for i in {1..10} ; do 
    echo $i 
done
or
for i in {1..10} ; do echo $i; done
  • loop through strings
for file in toto.txt tata.txt tutu.txt titi.txt ; do
     echo $file
done
  • loop through all the files in a directory
dir="/enwiki/0/00"
for file in $dir/* ; do
    echo "$file"
done
or
dir="/enwiki/0/00"
for file in `ls -1 $dir` ; do
    echo $file
done

Sort lists

Alphabetically

  • to generate a sorted list of the files on the Desktop:
ls Desktop | sort

Numerically

  • to sort a list of numbers.
for i in 10 1 3 5 6 2 4 7 9 ; do echo $i ; done | sort -n
  • to remove replicated numbers
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