Shell Scripting Flashcards

1
Q

Executing a script
A. Writing it directly in bash interpreter

B. First write the script then execute

A

A.
1. Go to directory where the script should live
2. Create a file with .sh extension
3. Write script in that file using an editor (e.g. vi)
Indicate the file is a bash script with #!/bin/bash
4. Make script executable >chmod 755 [script name].sh
5. Run script > ./[script name].sh

B.

  1. Start script with : #!/bin/bash
  2. Apply executable permission to .sh file > chmod 755 [script name].sh
  3. Run script
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Shell script with variable in script

A
#! /bin/bash
MY_SHELL="eat"
echo "I am ${MY_SHELL}ing my keyboard."

Output
I am eating my keyboard

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Shell script with output of a command as variable

A

!# /bin/bash
SERVER_NAME = $(hostname) &raquo_space;this is the command
echo “You use ${hostname}”

Output
You use Linux

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Executing Python scripts via the interpreter

A
  1. Write python script
    > #! /usr/bin/python
    > print “THis is a python script”
  2. Add execute permissions
    >chmod 755 hi.py
  3. Run script from present directory
    ./hi.py
    This is a Python script.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

File Operators

A
  • d FILE True if file is a directory.
  • e FILE True if file exists.
  • f FILE True if file exists and is a regular file.
  • r FILE True if file is readable by you.
  • s FILE True if file exists and is not empty.
  • w FILE True if the file is writable by you.
  • x FILE True if the file is executable by you
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

String Operators

A

-z STRING True if string is empty.
-n STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Arithmetic operators

A

arg1 –eq arg2 True if arg1 is equal to arg2.
arg1 –ne arg2 True if arg1 is not equal to arg2.
arg1 –lt arg2 True if arg1 is less than arg2.
arg1 –le arg2 True if arg1 is less than or equal to arg2.
arg1 –gt arg2 True if arg1 is greater than arg2.
arg1 –ge arg2 True if arg1 is greater than or equal to arg2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

IF Statement Syntax

A
if [ condition-is-true ]
then
     command 1
     command 2
     command N
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

IF Statement script - Example

A
#!/bin/bash
MY_SHELL="bash"
if [ "$MY_SHELL" = "bash" ]
then
    echo "You seem to like the bash shell."
fi

Output:
You seem to like the bash shell.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

IF/THEN/ELSE Syntax

A
if [ condition-is-true ]
then
     command N
else
      command N
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

IF/THEN/ELSE Script Example

A
#!/bin/bash
MY_SHELL="csh"
if [ "$MY_SHELL" = "bash" ]
then
   echo "You seem to like the bash shell."
else
    echo "You don't seem to like the bash
shell."
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

IF / ELIF / ELSE Syntax

A
if [ condition-is-true ]
then
  command N
elif [ condition-is-true ]
then
    command N
else
    command N
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

IF / ELIF / ELSE Example

A

!/bin/bash

MY_SHELL=”csh”
if [ “$MY_SHELL” = “bash” ]
then
echo “You seem to like the bash shell.”
elif [ “$MY_SHELL” = “csh” ]
then
echo “You seem to like the csh shell.”
else
echo “You don’t seem to like the bash or csh shells.”
fi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

FOR Loop Syntax

A
for VARIABLE_NAME in ITEM_1 ITEM_N
do
    command 1
    command 2
    command N
done
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

FOR Loop Script Example

A
#!/bin/bash
for COLOR in red green blue
do
     echo "COLOR: $COLOR"
done
Output:
COLOR: red
COLOR: green
COLOR: blue
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
#!/bin/bash
COLORS="red green blue"
for COLOR in $COLORS
do
     echo "COLOR: $COLOR"
done
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

FOR Loop Script Example 2

A
#!/bin/bash
PICTURES=$(ls *jpg)
DATE=$(date +%F)
for PICTURE in $PICTURES
do
echo "Renaming ${PICTURE} to ${DATE}
-${PICTURE}"
mv ${PICTURE} ${DATE}-${PICTURE}
done
$ ls
bear.jpg man.jpg pig.jpg rename-pics.sh
$ ./rename-pics.sh
Renaming bear.jpg to 2015-03-06-bear.jpg
Renaming man.jpg to 2015-03-06-man.jpg
Renaming pig.jpg to 2015-03-06-pig.jpg
$ ls
2015-03-06-bear.jpg 2015-03-06-man.jpg
2015-03-06-pig.jpg rename-pics.sh
17
Q

Positional Parameters Syntax

A

$ script.sh parameter1 parameter2 parameter3

$0 : “script.sh”
$1 : “parameter1”
$2 : “parameter2”
$3 : “parameter3”

18
Q

Positional Parameters script Example

A
#!/bin/bash
echo "Executing script: $0"
echo "Archiving user: $1"
# Lock the account
passwd –l $1
# Create an archive of the home directory.
tar cf /archives/${1}.tar.gz /home/${1}

$ ./archive_user.sh elvis
Executing script: ./archive_user.sh
Archiving user: elvis
passwd: password expiry information changed.
tar: Removing leading `/’ from member names

19
Q

Accepting User Input (STDIN) Syntax

A

The read command accepts STDIN.

read -p “PROMPT” VARIABLE

20
Q

Accepting User Input (STDIN) Example

A

!/bin/bash

read –p “Enter a user name: “ USER
echo “Archiving user: $USER”

# Lock the account
passwd –l $USER

Create an archive of the home directory.
tar cf /archives/${USER}.tar.gz /home/${USER}
____________________
$ ./archive_user.sh
Enter a user name: mitch
Archiving user: mitch
passwd: password expiry information changed.
tar: Removing leading `/’ from member names