The Script/Linux
Bash Scripting
Bash is a programming language commonly used to automate tasks in Linux because it's included in nearly every distribution of Linux. In fact any Linux terminal that you use is probably using bash to interpret what you type, but it can also be used non-interactively by writing Bash code into a file and executing it all at once. If you've programmed in other languages before, know that Bash's syntax is very different to other languages. The main reason that it's often better to write code for CyberPatriot in Bash instead of, for example, Python, is because it gives you easy access to many commands that are useful for interacting with the operating system, such as starting and stopping services, and the bash interpreter is preinstalled on almost all distributions of Linux. However Python should probably be used for scripts with more complex logic.
Here's an example of a bash program that asks the user for a password and compares it to a preset password.
printf "Enter your password: " read input if [[ "$input" == "secretpassword" ]]; then printf "correct\n" else printf "incorrect\n" fi
Here's a breakdown of how this code works:
printfis a command which prints text to the screen. The text is specified by its first argument.readis a command which takes user input, and puts it into a variable, specified by its first argument. The variable is named "input" here, but it can be named anything.- Next there's an "if statement". "if statements" start with the keyword "if", then a condition enclosed in double brackets (There must be a blank space surrounding both sides of the variables within), and lastly a semicolon and the keywork "then". This is equivalent to an opening brace in other programming languages. The code placed after the if statement but before the keyword "else", is executed if the condition is true. The code after "else", but before "fi", is executed if the condition is false. The indentation is not required.
printfis used again to printf "correct" or "incorrect", but these times it includes a '\n' at the end, which represents a new line.
Here's an example of another short script, but this time it's more relevant to CyberPatriot. It finds every user on the computer and changes their password to something.
users=$(grep "sh$" /etc/passwd | cut -d':' -f1)
for user in ${users[@]}; do
printf "SuperSecretPassword\nSuperSecretPassword\n" | passwd $user
done
In Progress