Labels

Arduino (1) Bugs (3) C (1) HTML (1) Install (3) Linux (27) Maths (1) Python (11) Raspberry Pi (4)

Thursday, 22 June 2017

How to add a user to the sudo group


Sometimes you need to be able to run commands or programs with elevated privileges, to do this the user needs to be in the sudo group.

To add them to the group run the following command as root or with a account with sudo access:
sudo usermod -aG sudo "username"
They should now be able to run commands with elevated privileges.

Monday, 29 May 2017

Command line parameters in shell scripts


Command line parameters are a way to pass information into a program or script in order for it to do what you want it to. Some examples of command line parameters:

ls -l
cat textfile
The command line parameters here are the "-l" and "textfile"

How are command line parameters accessed within a shell script?
They are stored in these variables:
"$0":This holds the name of the command.
"$1":This holds the first parameter.
"$2":This holds the second parameter.
"$3":This holds the third parameter and the pattern repeats.
"$#":This holds the number of parameters that have been passed.
"$@":This holds all of the parameters

Lets make a simple script call it parameters.sh:
#!/bin/sh
echo "Name of script:$0"
echo "First parameter:$1"
echo "Second parameter:$2"
echo "Number of parameters:$#"
echo "All parameters:$@"
When we run the script you should get the following output
Input: 
sh parameters.sh hello world
Output:
Name of script:parameters.sh
First parameter:hello
Second parameter:world
Number of parameters:2
All parameters:hello world



How to format code within web pages


When displaying code within web pages it can be hard to read if it hasn't been separated from the other text and images on the page. To get around this problem I use the google code-prettify JavaScript.
This allows code to be displayed like this:
#!/bin/sh
echo "Hello, World!"

To implement this within your HTML you need to add the following lines of HTML:
At the start of your page:

<script src="https://cdn.rawgit.com/google/code-prettify
/master/loader/run_prettify.js"></script><script>

Then whenever you want to display code:

<pre class="prettyprint">
#!/bin/sh
echo "Hello, World!"
</pre>

Wednesday, 15 March 2017

Python Quadratic Formula


A equation with the form ax^2+bx+c=0 is known as a quadratic equation. When plotted on a graph it will take the general shape of the graph seen below. The points at which the quadratic graph crosses the x-axis are known as the solutions of the quadratic equation.















For this example the quadratic crosses the x-axis when x=3 and when x=-1.

The quadratic formula is used to find the values that satisfy the equation.
Quadratic Formula






I wrote a python script which can find the solutions of these quadratics by using the quadratic formula.

import math
print "ax^2+bx+c=0"

a = float(raw_input("Please enter the value of a: "))
b = float(raw_input("Please enter the value of b: "))
c = float(raw_input("Please enter the value of c: "))

try:
  x1 = (-b + math.pow((math.pow(b,2)-(4*a*c)),0.5)) / 2*a
  x2 = (-b - math.pow((math.pow(b,2)-(4*a*c)),0.5)) / 2*a
except ValueError:
  print "No real solutions"
  exit()

print  "X solutions: ", x1, ",",x2

The Quadratic formula has been wrapped in a try statement to prevent the program from crashing if a quadratic equation with no real solutions is entered. This would cause a crash as you would be trying to find the square root of a negative number which is not possible.

Wednesday, 22 February 2017

Check root at start of script


Some scripts need to be run as root and you may want to check at the start of that script that it is running as root. This can be done by checking the environment variable $EUID. This variable will hold the value 0 if it's being run as root.
#!/bin/bash
if [ "$EUID" -ne 0 ];then
echo "Please run this script as root"
exit 1
fi