Marius van Witzenburg We fight for our survival, we fight!

17jul/110

How to check if a key exists in an array with in_array for Bash

Posted by mariusvw

When working with Bash it might become handy to have a way to check if a record exists in an array. In PHP you have in_array for this... The below code adds a function similar to the PHP variant.

function in_array() {
  local x
  ENTRY=$1
  shift 1
  ARRAY=( "$@" )
  [ -z "${ARRAY}" ] && return 1
  [ -z "${ENTRY}" ] && return 1
  for x in ${ARRAY[@]}; do
    [ "${x}" == "${ENTRY}" ] && return 0
  done
  return 1
}
 
MASTER=()
CURRENT=()
FIRST=1
for SERVER in ${SERVERS}; do
  # collect all builds from server and populate CURRENT list
  COMMAND="${LS} -1fd ${WEBROOT}/${SITE}.*"
  BUILDS=`${SSH} ${SSHOPTS} root@${SERVER} "${COMMAND}"`
  for BUILD in ${BUILDS}; do
    CURRENT=( ${CURRENT[@]-} ${BUILD} )
  done
 
  # if this is our first time around, copy CURRENT to MASTER
  if [ ${FIRST} -eq 1 ]; then
    MASTER=( ${CURRENT[@]} )
    FIRST=0
  fi
 
  # now we do a compare between MASTER and CURRENT to see what builds
  # are common
  INTERSECT=()
  for ENTRY in ${CURRENT[@]}; do
    in_array "${ENTRY}" "${MASTER[@]}"
    RET=$?
    if [ "${RET}" -eq 0 ]; then
      INTERSECT=( ${INTERSECT[@]-} ${ENTRY} )
    fi
  done
  MASTER=( ${INTERSECT[@]} )
 
  # clear the CURRENT array
  CURRENT=()
done

Source: http://mykospark.net/tag/in_array/

Geëtiketeerd als: , , , Geen reacties
10jul/110

How to read a file line by line with Bash

Posted by mariusvw

Sometimes you want to do actions per line instead of the complete file at once.

Here is an example that you can use to read the file line by line.

#!/bin/bash
run_cmd_file() {
    while read line
    do
        chr=${line:0:1}
        case $chr in 
            "#" )  # ignore commented lines
                ;;      
            *   )   
                echo line[$line]"
                ;;      
        esac    
    done < $2
}
 
run_cmd_file filename
Geëtiketeerd als: , , , , , Geen reacties
19jun/110

How to add bots to your Urban Terror server via RCON

Posted by mariusvw

First of all you need to configure your server first! For more info about that check here.

Now you should have your server all up and running, next thing is having control over your bots.

bot configuration

First enable the use of bots

/rcon bot_enable 1
/rcon reload

Next, lets add bots.

/rcon addbot <type> [level] [team] [ping] [nick]

addbot arguments

type
Define the name of the bot type here, chose one from the list below.
level
Define a number from 1 to 5 here to set how good the bots will try to hunt you down!
team
Define the color name Red or Blue to set the team this bot belongs to.
ping
Define the average ping time the bot will have, if you define this to a low value the bots will respond fast, I recommend setting this to 40 on a local LAN and 75 on a WAN connection.
nick
Define the nickname of the bot, I usually give a simple name like Bot1, Bot2, Bot3, etc...

addbot examples

/rcon addbot Chicken Blue 75 Bot1
/rcon addbot Cockroach Blue 75 Bot2
/rcon addbot Mantis Blue 75 Bot3

In case you want to get rid of one or all bots, simply kick them!

/rcon kick [nickname]
/rcon kick allbots

Here is a simple Bash example that you could use to add or remove bots:

#!/bin/bash
 
# Add bot
echo -e "\xFF\xFF\xFF\xFFaddbot Chicken Blue 75 BOTNAME\n" | /usr/bin/nc -D -4 -d -n -u 127.0.0.1 27960
 
# Remove bot
echo -e "\xFF\xFF\xFF\xFFkick BOTNAME\n" | /usr/bin/nc -D -4 -d -n -u 127.0.0.1 27960


Here is a list of the bots names, weapons and their accessories they are carrying.

Name: Boa
Primary: ZM LR300 ML
Secondary: H&K MP5K
Sidearm: .50 Desert Eagle
Grenades: HE Grenades
Item 1: Kevlar Vest
Item 2: -
Item 3: -
 
Name: Cheetah
Primary: Kalashnikov AK103
Secondary: Franchi SPAS-12
Sidearm: .50 Desert Eagle
Grenades: HE Grenades
Item 1: Kevlar Vest
Item 2: -
Item 3: -
 
Name: Chicken
Primary: H&K G36
Secondary: H&K MP5K
Sidearm: .50 Desert Eagle
Grenades: HE Grenades
Item 1: Kevlar Vest
Item 2: -
Item 3: -
 
Name: Cobra
Primary: ZM LR300 ML
Secondary: H&K MP5K
Sidearm: .50 Desert Eagle
Grenades: Smoke Grenades
Item 1: Kevlar Vest
Item 2: -
Item 3: -
 
Name: Cockroach
Primary: H&K UMP45
Secondary: -
Sidearm: Beretta 92G
Grenades: Flash Grenades
Item 1: Kevlar Vest
Item 2: -
Item 3: -
 
Name: Cougar
Primary: H&K G36
Secondary: -
Sidearm: Beretta 92G
Grenades: Flash Grenades
Item 1: Silencer
Item 2: Kevlar Vest
Item 3: -
 
Name: Goose
Primary: H&K 69
Secondary: H&K UMP45
Sidearm: .50 Desert Eagle
Grenades: -
Item 1: Extra Ammo
Item 2: Kevlar Vest
Item 3: -
 
Name: Mantis
Primary: ZM LR300 ML
Secondary: -
Sidearm: Beretta 92G
Grenades: -
Item 1: Laser Sight
Item 2: Silencer
Item 3: Kevlar Vest
 
Name: Penguin
Primary: ZM LR300 ML
Secondary: -
Sidearm: .50 Desert Eagle
Grenades: -
Item 1: Laser Sight
Item 2: Silencer
Item 3: Kevlar Vest
 
Name: Puma
Primary: ZM LR300 ML
Secondary: -
Sidearm: Beretta 92G
Grenades: -
Item 1: Laser Sight
Item 2: Silencer
Item 3: Kevlar Vest
 
Name: Python
Primary: H&K G36
Secondary: Franchi SPAS-12
Sidearm: .50 Desert Eagle
Grenades: HE Grenades
Item 1: Kevlar Vest
Item 2: -
Item 3: -
 
Name: Raven
Primary: H&K PSG-1
Secondary: H&K MP5K
Sidearm: .50 Desert Eagle
Grenades: -
Item 1: Kevlar Vest
Item 2: Silencer
Item 3: -
 
Name: Scarab
Primary: H&K G36
Secondary: H&K MP5K
Sidearm: .50 Desert Eagle
Grenades: -
Item 1: Kevlar Vest
Item 2: Silencer
Item 3: -
 
Name: Scorpion
Primary: Remington SR8
Secondary: H&K MP5K
Sidearm: Beretta 92G
Grenades: HE Grenades
Item 1: Kevlar Vest
Item 2: -
Item 3: -
 
Name: Tiger
Primary: Kalashnikov AK103
Secondary: -
Sidearm: Beretta 92G
Grenades: HE Grenades
Item 1: Medkit
Item 2: Kevlar Vest
Item 3: -
 
Name: Widow
Primary: ZM LR300 ML
Secondary: H&K MP5K
Sidearm: Beretta 92G
Grenades: -
Item 1: Kevlar Vest
Item 2: Laser Sight
Item 3: -
Geëtiketeerd als: , , , , , , Geen reacties
14jun/111

How to convert a string to lower case or UPPER case with Bash

Posted by mariusvw

Ever had a situation where you want the output or input in a BASH script to be lowercase or uppercase whatever happens but you couldn't find a way how to do that? You can!

Simply, use these two functions in your script:

1
2
3
4
5
6
7
8
9
10
toLower() {
  echo $1 | tr "[:upper:]" "[:lower:]" 
} 
toUpper() {
  echo $1 | tr "[:lower:]" "[:upper:]" 
}
 
# Example
echo `toUpper hey`
echo `toLower HEY`

I hope this helps you out with writing your precious script ;-)

Geëtiketeerd als: , , , , , , 1 Reactie
14jun/110

How to download a range of files with Curl and Bash

Posted by mariusvw

This is a really simple sniped how to download a range of files.

I found this very useful to download an image range of my old avatar script.
I lost my images but remembered an url where I uploaded them once. Happy me :-)

1
2
3
4
5
6
7
#!/usr/local/bin/bash
 
# Range 1 to 75
for x in {1..75}
do
    curl -O http://my.server.com/media/images_${x}.jpg
done
Geëtiketeerd als: , , , , Geen reacties
14jun/110

How to clear Bash shell history when you log out of the command-line

Posted by mariusvw

On some machines I prefer to not leave a log of everything I typed on the command-line. So, I remove the logs and clear the history.
To accomplish this you have to add the lines below to ~/.bash_logout:

history -c
rm -f ~/.bash_history
rm -f ~/.history

Geëtiketeerd als: , , Geen reacties
14jun/110

How to batch rename files and use the file its own name content with Bash

Posted by mariusvw

In my example the files had the right content in their name only the position was wrong.

The original files their name were:

The file title - vendor_1.mp3
The file title - vendor_4.mp3
The file title - vendor_22.mp3
The file title - vendor_39.mp3

But afterwards I found this is a difficult way of searching on a vendor. Which I do the most.
So, I decided to make a simple rename script to move the vendor to the beginning of the file name and the title to the end of the filename.

This would give the following filenames as result:

vendor_1 - The file title.mp3
vendor_4 - The file title.mp3
vendor_22 - The file title.mp3
vendor_39 - The file title.mp3

I hope you find this useful or that you can modify it for your own use. You can see the tiny script I used below.

#!/usr/local/bin/bash
 
root="/my/files/location"
 
find $root -type f | while read file
do
    filename=${file##*/}
    piece1=${filename%% - *}
    piece2=${filename##* - }
    extension=${filename##*.}
    newfile="${piece2%%.$extension} - ${piece1}.$extension"
    mv "${file}" "${root}/${newfile}"
done
Geëtiketeerd als: , , , , Geen reacties
14jun/111

How to send a message to your iPhone via Pushme.to with PHP or Bash and Curl

Posted by mariusvw

After having Pushme.to installed on my iPhone for over 3 months now I finally had the time to work on some scrips to make it work in different languages for use on servers that I manage.

The PHP version is the less version I use but may be handy for some people.

PHP Version:

function pushMeTo($username, $message, $signature) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'http://pushme.to/' . $username . '/'); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'message=' . urlencode($message) . '&signature=' . urlencode($signature));
    curl_exec($curl);
    curl_close($curl);
}
pushMeTo('username', 'Hello world!', 'God');

A more used version is the commandline (Bash) version:

Bash version:

#!/bin/bash
username="username"
message="Hello world"
signature="God"
/usr/local/bin/curl -F "message=$message" -F "signature=$signature" "http://pushme.to/$username/"

I currently use these for server monitoring on FreeBSD like the GEOM Raid bay and S.M.A.R.T. status of the disks and website monitoring which works quite nice. And in case the internet connection drops I have a backup via SMS with a serial connected cellphone. So this should be quite failsafe :-)

If you have any other language that you want to use such as Perl just let me know and I could write it for you.

I hope you find these scripts useful. Happy texting! :-)

10jun/110

How to handle filenames containing whitespace in Bash

Posted by mariusvw

First capture the current IFS in $SAVEIFS and then replace it with a newline instead of spaces, now do your thing and afterwards restore the IFS with $SAVEIFS.

And yes, you can not use new lines in filenames ;-)

#!/usr/local/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *
do
    echo "$f"
done
IFS=$SAVEIFS
Geëtiketeerd als: , , Geen reacties
18mei/100

How to trap or catch Keyboard Interrupt in Bash on Linux/FreeBSD

Posted by mariusvw

This is a simple code snippet, I think it explains it self.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
 
myCleanup() {
  rm -f /myapp/tmp/mylog
  return $?
}
 
myExit() {
  echo -en "n*** Exiting ***n"
  myCleanup
  exit $?
}
 
trap myExit SIGINT
 
# main loop
while true
do
    echo -n "Enter your name: "
    read x
    echo "Hello $x"
done

If you have any questions, just ask :-)

Page 1 of 212