Professional Documents
Culture Documents
Access Permissions
• One can easily view the permissions for a file by invoking a long
format listing using the command ls -l.
• For instance, if the user juan creates an executable file named test,
the output of the command ls -l test would look like this:
• The permissions for this file are listed are listed at the start of the line,
starting with rwx.
• This first set of symbols define owner access.
• The next set of rwx symbols define group access
• The last set of symbols defining access permitted for all other users.
Access Permissions
• This listing indicates that the file is readable, writable, and executable
by the user who owns the file (user juan) as well as the group owning
the file (which is a group named student).
• $ ls –l
Moving in Directories
• cd try_it
– Changes the directory to try_it
• pwd
– Prints present working directory (e.g. /home/smith/try_it)
• cd .. Move to superior directory
pwd : Prints /home/smith
• cd /home The absolute path
pwd : Prints /home
• cd The system is returned to the user home directory
pwd : Print /home/smith
Make Directory
• copies file_1 to file_2. The both files must be in the same working
directory. If they are in various directories, the path must be given.
Rename and/or Move the File
rm h*c
• you will remove all files beginning with h and ending with c which are
in working directory.
Remove File
• If you write
rm *
• The group of the file or directory can be changed using the command
• -R is optional and when used with directories will traverse all the sub-
directories of the target directory changing ALL the permissions to ###.
Access Permission of File/Directory
• The #'s can be:
0 = Nothing
1 = Execute
2 = Write
3 = Execute & Write (2 + 1)
4 = Read
5 = Execute & Read (4 + 1)
6 = Read & Write (4 + 2)
7 = Execute & Read & Write (4 + 2 + 1)
Shell Scripting
Shell script/program
• A series of shell commands placed in an ASCII text file
• Commands include
• Anything you can type on the command line
• Shell variables
• Control statements (if, while, for)
Script execution
• Provide script as an argument to the shell program (e.g. bash
my_script)
• Or specify which shell to use within the script
• First line of script is #!/bin/bash
• Make the script executable using chmod
• Make sure the PATH includes the current directory
• Run directly from the command line
• No compilation is necessary!
Simple example script
#!/bin/bash Output:
Hello world!
echo “Hello world!” /home/grads/sgifford
cd ~
pwd
Quoting
• Quoting is necessary to use special characters in a variable’s value or
string
• ”” - shell only interprets $ and ‘‘ and \
• $ - variable substitution
• ` - Command substitution
• \” - Literal double quote
• \ is used to escape characters
• echo ‘`date +%D`‘ will print: 10/06/03
• ’’ - shell doesn’t interpret special characters
• echo ‘`date +%D`‘ will print: `date +%D`
Shell variables
Numeric • var refers to the name, $var
to the value
Strings • t = 100 #Sets var t to value
Arrays 100
• echo ”\$t = $t” #will print:
Command line arguments $t = 100
Functions • Remove a variable with unset
var
Read only • Names begin with alpha
characters and include alpha,
numeric, or underscore
Numeric variables
• Integer variables are the only pure numeric variables that can be used
in bash
• Declaration and setting value:
declare -i var=100
• Expressions in the style of C:
• (( expression ))
• e.g. (( var+=1 ))
• +, -, *, /, %, &, |, ~, <, >, <=, >=, ==, !=, &&, ||
String variables
• If you do not use the declare keyword with option –i when
using a variable for the first time, it will be a string
• var=100 makes var the string ‘100’.
• However, (( var=100 )) will treat var as an integer even though it is a
string
Array variables
• Array is a list of values
• Don’t have to declare size
• Reference a value by ${name[index]}
• ${a[3]}
• $a (same as ${a[0]})
• Use the declare -a command to declare an array
• declare –a sports
• sports=(ball frisbee puck)
• sports[3]=bat
Arrays
• Array initialization
• sports=(football basketball)
• moresports=(${sports[*]} tennis)
• ${array[@]} or ${array[*]} refers to the entire array
contents
• echo ${moresports[*]} produces
football basketball tennis
Command line arguments
• If arguments are passed to a script, they can be referenced by $1, $2,
$3, …
• $0 refers to the name of the script
• $@ - array filled with arguments excluding $0
• $# - number of arguments
Exporting variables
• The export command, when used with a variable name, allows
child processes of the shell to access the variable
Output
• We have already seen echo
• More common in other shells including ksh is print (does not exist
in bash)
• echo –n does not print newline after output
Return values
• Scripts can return an integer value
• Use exit N
• The variable $? will contain the return value of the last command run
• Can be used to test conditions
Conditions
• If using integers: (( condition ))
• If using strings: [[ condition ]]
• Examples:
• (( a == 10 ))
• (( b >= 3 ))
• [[ $1 = -n ]]
• [[ ($v != fun) && ($v != games) ]]
• Special conditions for file existence, file permissions, ownership, file
type, etc.
Conditions (continued…)
• [[ -e $file]] – File exists?
• [[ -f $file]] – Regular file?
• [[ -d $file]] – Directory?
• [[ -L $file]] – Symbolic link?
• [[ -r $file]] – File has read permission?
• [[ -w $file]] – File has write permission?
• [[ -x $file]] – File has execute perm?
• [[ -p $file]] – File is a pipe?
If statements
• Syntax:
if condition
then
statements
elif condition
then
statements
else
statements
fi
If statement
• Example
if [[ -r $fname ]]
then
echo ’$fname is readable’
elif [[ -w $fname && -x $fname ]]
then
echo ’$fname is writeable and executable’
fi
For loops
• Syntax:
for var [in list]
do
statements
done
• If list is omitted, $@ is assumed
• Otherwise ${list[*]}
• where list is an array variable.
For loop example
for colors in Red Blue Green Yellow Orange Black Gray
White
do
echo $colors
done
echo
While loops
• Syntax:
while condition
do
statements
done
• The keywords break, continue, and return have the same
meaning as in C/C++
Case statements
• Syntax:
case expression in
pattern1)
statements ;;
pattern2)
statements ;;
…
*)
statements ;;
esac
Case statement example
case $1 in
-a)
statements related to option a ;;
-b)
statements related to option b ;;
*)
all other options ;;
esac
Command substitution
• Use the output of a command in a variable, condition, etc. by:
• `command`
• $(command)
Examples
• Arguments printed in a for loop
• Reformatting the wc command
• Performing a depth-first search