July 31

Linux: How to write bash while-loops

You can execute a sequence of commands by writing them into a “script file” and then running the script file. A script file is simply a text file that contains a sequence of instructions that could also be executed from the command line (also know as shell). Usually the extension “.sh” is used for script files.

Here is an example of a while loop:

#!/bin/bash
count=1
while [ $count -le 9 ]
do
echo “$count”
sleep 1
(( count++ ))
done

When executed, this script file will print the numbers 1 through 9 on the screen. The while-statement gives you more flexibility for specifying the termination condition than the for-loop. For example you can make the previous script an infinite loop by omitting the increment statement “(( count++ ))”:

#!/bin/bash
count=1
while [ $count -le 9 ]
do
echo “$count”
sleep 1
done

The “sleep 1” statement pauses the execution for 1 second on each iteration. Use “Ctrl-C” to terminate the process.

You can also create an infinite loop by putting a colon as the condition:

#!/bin/bash
count=1
while :
do
echo “$count”
sleep 1
(( count++ ))
done

In order to use multiple conditions in the while-loop you need to use the double square bracket notation:

count=1
done=0
while [[ $count -le 9 ] && [ $done == 0 ]]
do
echo “$count”
sleep 1
(( count++ ))
if [ $count == 5 ]; then $done=1
fi
done

In this script the variable “done” is initialized to 0 and then set to 1 when count reaches 5. The loop condition states that the while loop will continue as long as “count” is less than nine and “done” is equal to zero. Therefore the loops exits when count equals 5.

The “&&” means logical “and” and “||” means logical “or”.

An alternative notation for the conjunctions “and” and “or” in conditions is “-a” and “-o” with single square brackets. The above condition

[[ $count -le 9 ] && [ $done == 0 ]]

could be rewritten as

[ $count -le 9 ] -a [ $done == 0 ]

Reading a text file is typically done with a while loop. In the following example, the bash script reads the contends of a file “inventory.txt” line be line:

FILE=inventory.txt
exec 6

The first line assigns the input file name to the variable “FILE”. The second line saves the “standard input” in the file descriptor “6” (it could be any value between 3 and 9). This is done so that “standard input” can be restored to file descriptor “0” at the end of the script (see statement “exec 0 In the 3rd line the input file is assigned to file descriptor “0”, which is used for standard input. The “read” statement then reads a line from the file on each iteration and assigns it to the variable “line1”.

In order to prematurely exit a while-loop you can use the break statement as in the following example:

count=1
done=0
while [ $count -le 9 ]
do
echo “$count”
sleep 1
(( count++ ))
if [ $count == 5 ]
then
break
fi
done
echo Finished

The break statement skips program execution to the end while loop and executes any statements following it. In this case the statement “echo Finished”.

The continue statement on the other hand skips only the rest of the while loop statement of the current iteration and jumps directly to the next iteration:

count=1
done=0
while [ $count -le 9 ]
do
sleep 1
(( count++ ))
if [ $count == 5 ]
then
continue
fi
echo “$count”
done
echo Finished

In this case the “continue” statement is executed when the variable “count” reaches 5. This means the subsequent statement (echo “$count”) is not executed on this iteration (when the value of “count” is 5).

By J. Haas

Category: Linux | Comments Off on Linux: How to write bash while-loops
July 16

Linux: Repair Grub Bootloader / locked out of bootloader

Locking down Grub with a password is a great idea,
but sometimes things go wrong and you might find yourself locked out from booting your computer.
The following example is from memory so I may have missed a step, but it should be close to how to fix this issue

Boot into a live Linux CD or USB
(Note: I have found it needs to be the same version of Linux, especially when using the newer versions of Ubuntu.)

Start a terminal session
Type: sudo fdisk -l
Find the boot partition of you linux installation (In this example we have it install on /dev/sda1)

sudo mount /dev/sda1 /mnt

Remove you changes in /mnt/etc/grub.d/00_header (or whichever grub configuration file you changed that caused the issue

Create some neccessary hardlinks
sudo mount –bind /dev /mnt/dev
sudo mount –bind /proc /mnt/proc
sudo mount –bind /sys /mnt/sys
sudo chroot /mnt
update-grub /dev/sda1
CTRL-D
sudo umount /mnt/dev
sudo umount /mnt/proc
sudo umount /mnt/sys
sudo umount /mnt

Category: Linux | Comments Off on Linux: Repair Grub Bootloader / locked out of bootloader
July 10

Ubuntu: Cannot connect to WPA & WPA2 Enterprise with PEAP

When using the wifi option WPA & WPA2 Enterprise with PEAP and no CA certificates, later versions of Ubuntu are failing to connect even though you tell it not to use a certificate and to ignore.
To fix this do the following:

In /etc/NetworkManager/system-connections/”connection-name”

remove: “system-ca-cert=true"

Category: Linux | Comments Off on Ubuntu: Cannot connect to WPA & WPA2 Enterprise with PEAP
June 24

Linux: Ubuntu GDBus error: Bluetooth failing

Ubuntu seems to currently (6/24/2014) have issues with the atheros bluetooth card.

Best fix so far:

I created a file [bluetooth-fix.conf] under /etc/modprobe.d with this line:
options ath9k btcoex_enable=1

Enabling bluetooth coexistence

Reboot


Also:

Bluetooth coexistence has to be manually enabled when loading ath9k by setting the btcoex_enable module parameter.

modprobe ath9k btcoex_enable=1

 

By: F Alduraibi

Category: Linux | Comments Off on Linux: Ubuntu GDBus error: Bluetooth failing
May 21

Linux: Virtualbox virtual disk resize and attachment

At this point resizing a Virtualbox disk image is done from the command line.

* This page assume you have already detached the virtual disk from your virtual machine

To turn a Windows.vdi from a 25 GB drive to a 30 GB drive

1. Start a terminal session
2. change directory to the directory your virtual disk is stored in
3. Enter: VBoxManage modifyhd Windows.vdi –resize 30720

VBoxManage modifyhd virtualdrivename –resize Megabytes

To reattach your virtual disk to a Virtual machine called Windows

1. Start a terminal session
2. change directory to the directory your virtual disk is stored in
3. Enter: VBoxManage storageattach Windows –storagectl “SATA” –device 0 –port 0 –type hdd –medium Windows.vdi

Category: Linux | Comments Off on Linux: Virtualbox virtual disk resize and attachment