Bash, quiz 3 Flashcards

1
Q

what is a shell?

A

A shell is a command interpreter, an interface between a
human (or another program) and the OS

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

what is the terminal?

A

The terminal is a program that opens a graphical window and
lets you interact with the shell.

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

what are the shell’s responsibilities?

A

Program execution
Variable and file name substitution
I/O Redirection
Pipeline Hookup
Environment Control
Interpreted Programming Languages

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

shell I/O redirection metacharacters

A

< > > > |

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

shell wildcard metacharacters

A

Asterix ? [ ]

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

shell other metacharacters

A

& ; $ ! \ ( ) space tab newline

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

shell substitutions/globbing

A
  • Matches 0 or more of any character – ls *.c
    ? Matches any single character – ls file0?.c
    […] Matches any single character if it is in list provided
    – ls file[0-9].c
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is a shell script?

A

A shell script is a file that contains shell commands

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

How do you specify the shell to execute the program?

A

The script must begin with #! (pronounced “shebang”) to identify shell to be executed
– #! /bin/bash

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

What are the steps to run a shell script?

A

To run:
– Make executable: chmod +x hello.sh
– Invoke via: ./hello.sh

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

What are the two types of variables that Bash supports?

A

Bash supports two types of variables:
– Local variables
– Environment variables,

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

How are environment variables set?

A

created using export[variable]

Environment variables are set by the system and can usually
be found using the env command
env [options] [varname=value] [command]
– Displays the current environment or modifies the specified variables
– Specified commands are executed in the new environment

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

what does the $PATH variable do?

A

Initially set when the shell is created (login process)
– Provides a list of available directories where an executable command may be found

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

List some shell scripting features

A

Full scripting language
– Conditional statements (if-then-else, case, …)
– Arithmetic, string, file, environment variables
– Input (prompt user, command-line arguments, …)
– Loop statements (for, while, do-while, repeat-until, …)
– Lists (and, or)
– Functions
– Traps

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

What is the syntax for declaring a local variable in bash?

A

Variable structure format
varname=value
– Variable name must begin with alphabetic or underscore character, followed by zero or more alphanumeric or underscore characters
– Note there should not be any spaces around the ‘=’ sign
– Variables are case-sensitive
– Do not use globbing metacharacters, such as ? and *, in your variable names
– Once defined, use by prefixing $ symbol to variable name

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

How do you export a Bash shell variable?

A
  • Variables can be exported
    – The value of the variable can be passed to other subshell
  • Export command format:
    export [varname]

– Changing an exported variable in a subshell does not change the value in the parent shell

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

how do you destroy an exported variable? [bash scripting]

A

by using ‘unset’

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

How does ‘export’ with no arguments behave in a bash script

A

export with no arguments lists the variables that are exported
to the user’s shell

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

How do you read in user input, bash scripting

A

The read command allows you to prompt for input and store it in a
variable
* Syntax:
read varname [varname1] [varname2] … [varnameN]
– or
read –p “prompt” varname1 [varname2] … [varnameN]
* Input entered by user are assigned to varname1, varname2,
etc.
* If more input is entered than there are variables, the
remaining input will be assigned to the last variable

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

quoting mechanisms, bash scripting

A

Quote characters and the backslash character have special
meaning in shell scripts
` perform command substitution ` uname `
“ allow some variable expansion, but prevent wildcard
replacement
‘ prevent wildcard replacement as well as variable and command
substitution
\ preserve the literal value of the next character

21
Q

how do you perform arithmetic evaluation in bash scripting?

A
  • let statement can be used to perform arithmetic operations
  • Available operators are: +, –, multiplication, /, %
    let X=10+2 times 7
    echo $X
    let Y=X+2 times 4
    echo $Y
  • An arithmetic expression can be evaluated by:
    $[expression] or $((expression))
    echo “$((123+20))”
    VALORE=$[123+20]
    echo “$[123*$VALORE]”
22
Q

what is the difference between single and double brackets, bash scripting?

A

Single brackets [ … ] are built-in in bash as an alias for the test command
Double brackets [[ … ]] is a bash keyword and is much more capable than single brackets

23
Q

test file operator commands, bash scripting
-d, -f, -r, -s, -w, -x, -G, -O, -u, -g

A
  • The test file operator returns true if:
    –d file: file is a directory
    –f file: file is an ordinary file, and exists
    –r file: file is readable by the process
    –s file: file is not empty
    –w file: file is writable by the process
    –x file: file is executable
    –G file: file is owned by the group user belongs to
    –O file: file is owned by the user
    –u file: set_user_id bit is set
    –g file: set_group_id bit is set
24
Q

bash numeric relational operators

A

greater than: -gt
greater than or equal: -ge
less than: -lt
less than or equal: -le
equal: -eq
not equal: -ne

25
Q

bash string relational operators

A

equal: = or ==
not equal: !=
str1 is less than str2: str1 < str2
str1 is greater than str2: str1 > str2
string length is greater than zero: -n str
string length is zero: -z str

26
Q

bash logical operators

A

! expression logical negation
expression1 –a expression2 logical and
expression –o expression logical or

The && (and) and || (or) logical operators may also
be used, but must be enclosed in [[ … ]]

27
Q

bash script example if… statement

A

if [[ condition ]]; then

fi

28
Q

bash script case statement

A

Syntax:
case value in
pattern1) statement-list1
;;
pattern2) statement-list2
;;

patternN) statement-listN
;;
esac

29
Q

bash script while loop

A

while [ expression ]
do
statement1
statement2

statementN
done

30
Q

bash script until loop

A

until [ expression ]
do
statement1
statement2

statementN
done

31
Q

bash script for loop

A

for varname in arg1 arg2 … argN
do
statement1
statement2

statementN
done

32
Q

bash script C style for loop

A

for (( expression1 ; expression2 ; expression3 ))
do
statement1
statement2

statementN
done

33
Q

bash script select command

A

select command constructs simple menu from a list
– Allows user to enter a number instead of a string value
– User enters sequence number corresponding to the argument in the
list
select value in LIST
do
statement(s)
done

34
Q

usage of break and continue in bash scripting

A
  • Interrupts the for, while, or until loop
  • The break statement
    – Transfers control to the statement after the done statement
    – Terminates execution of the loop
  • The continue statement
    – Transfers control to the done statement
    – Skips the test statements for the current iteration
    – Continues execution of the loop
35
Q

bash script shell function format

A

A shell function is similar to a shell script
– Stores a series of commands for execution later
– Shell stores functions in memory
– Shell executes a shell function in the same shell that called it (not a
subshell)
* Must be defined before they can be referenced
– Usually placed at the beginning of the script
function-name()
{
statements
}

36
Q

What does the following line in a Bash script do?

chmod +x script.sh

A

Makes script.sh executable

37
Q

Which command is used to set an environment variable in Bash?

A

export

38
Q

Which symbol is used to comment a line in a Bash script?

A

#

39
Q

!/bin/bash

What does the following Bash script do?

if [ -e myfile.txt ]; then
echo “File exists.”
else
echo “File does not exist.”
fi

A

Checks if a file exists

40
Q

Which of the following is a correct way to compare two numeric values using [ ] in Bash?

A

[ 3 -lt 8 ]

41
Q

What does the expression (( 3 * 4 )) evaluate to in Bash?

A

12

42
Q

Which command is used in Bash to evaluate and compute the result of an arithmetic expression?

A

let

43
Q

Which construct in Bash is used to create a menu for selecting options from a list?

A

select

44
Q

What is the correct syntax for evaluating an arithmetic expression in Bash?

A

(( expression ))

45
Q

In bash, if
a=someletters_12345_moreletters.txt
b=${a:12:5}

What would be your next statement to print the value of b

A

echo $b

46
Q

! /bin/bash

What will be the output of this program?

global=”pretty good variable”

checkvar()

{

local inside=”not so good variable”

echo $inside

global=”better variable”

echo $global

}

echo $global

checkvar

echo $global

checkvar

A

pretty good variable
not so good variable
better variable
better variable
not so good variable
better variable

47
Q

In Bash, which command is used to send the “interrupt” signal to a running process?

A

kill -2

48
Q

The output of this program
#!/bin/bash
string1=”Hello”
string2=”World”
echo “$string1.$string2”

A

Hello.World