Linux command line Flashcards
What is the Shebang?
The #! syntax used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. Most Linux shell and perl / python script starts with the following line
/usr/bin/bash
/usr/bin/sh
/usr/bin/python
What is a bashrc file?
.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.
You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.
What is the bin directory?
/bin is a standard subdirectory of the root directory in Unix-like operating systems that contains the executable (i.e., ready to run) programs that must be available in order to attain minimal functionality for the purposes of booting (i.e., starting) and repairing a system.
Name some standard root directories
/boot, /dev, /etc, /home, /mnt, /usr, /proc and /var.
Why is /bin in $PATH
bin is by default in PATH, which is the list of directories that the system searches for the corresponding program when a command is issued. This means that any executable file (i.e., runnable program) in /bin can be run just by entering the file name at the command line and then pressing the ENTER key. The contents of PATH can be seen by using the echo command as follows
What is $PATH?
$PATH is an file location related environment variable.
When one types a command to run, the system looks for it in the directories specified by PATH in the order specified.
You can view the directories specified by typing echo $PATH in the terminal.
Suppose there is a executable file foobar01.sh present at /home/user/foo1/foo2/foobar01.sh which you want to execute on a regular basis. typing the entire “path” would be time consuming. So we add the directory in to $PATH variable and we can execute foobar.sh directly without even specifying the path.
What is the command to add a directory to your PATH variable?
export PATH=$PATH:/home/user/foo1/foo2
How do you define a variable in a bash script?
count=5
No spaces!
Refer to a variable with the $ prefix e.g. echo $count
How do you add an argument linux script?
INPUT_DIR=$1
OUTPUT_DIR=$2
TEST=$3
….
How do you run a bash script?
./myscript arg1 arg2 arg3
How do you print a variable to the terminal?
echo $PATH
How do you convert a directory to its absolute path?
readlink -f $INPUT_DIR
How do you complete an arithmetic if statement?
count=5
if (($count == ‘5’ )); then
echo ‘yaho’
fi
or if you are executing a command conditional
if [ ! -d “$OUTPUT_DIR” ]; then
mkdir $OUTPUT_DIR
fi
Alway close with fi
Can use else and ifelse
How do you complete a for loop in bash scripting?
count=5
INPUT_DIR=$1
OUTPUT_DIR=$2
for i in $( find $INPUT_DIR -name ‘*.fastq.gz’); do
cp $i $OUTPUT_DIR
done
or
for i in $(seq $count); do
echo $1
done
- Always use $(command)
- Always use ; do
- Always end with done