Marius van Witzenburg

Life can be so beautiful, if you have a wide vision.

Menu
  • Home
  • FreeBSD
  • How to
  • FAQ
  • Projects
  • Things I like

Tag: Bash

How to make a simple spinner in Bash

By mariusvw
|
November 19, 2013
| No Comments
| How to

This seems the simplest way to make a spinner. To stop it, use the break (ctrl+c) 😉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env bash
 
# Hide cursor
echo -en "\033[?25l"
 
# Spin spin spin...
i=1
sp='\|/-'
delay=0.15
 
while :
do
    printf "\b${sp:i++%${#sp}:1}"
    sleep $delay
done

Read More »

Posted in How to Tagged Bash, Script, Spinner Leave a comment

Bash getopt versus getopts

By mariusvw
|
February 24, 2013
| No Comments
| FAQ

I recently reviewed someone’s bash code, and noted their use of getopt. I had always been using getopts, so was at first confused (due to syntax), then puzzled: which one is better, getopt or getopts? Getopt is older, but is a separate binary. It tends to be pretty robust, and supports long options (i.e., you […]

Read More »

Posted in FAQ Tagged Bash, Getopt, Getopts, Options Leave a comment

How to find “dead” symbolic links with a simple Bash script

By mariusvw
|
November 4, 2012
| No Comments
| How to

In a previous post I talked about how to find dead symbolic links on your filesystem. You can read about that here: How to find “dead” symbolic links I’d liked to be able to run this command with less typing, so I created the following script:

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh
#
# Finds and optionally deletes dead symlinks.
#
# Marius van Witzenburg <info@mariusvw.com>
 
if [ ! -z "$1" ] && [ "$1" == "-d" ]
then
    echo "Deleting dead symlinks..."
    read -p "Are you sure? [y/n]" -n 1 -r
    echo ""
    if [ "$REPLY" == "y" ]
    then
        find -L . -type l -print0 | xargs -0 rm
    else
        echo "Stopped."
    fi
else
    echo "Listing dead symlinks..."
    find -L . -type l
fi

Save it to /usr/local/bin/deadlinks or ~/bin/deadlinks Set the right […]

Read More »

Posted in How to Tagged Bash, Dead, Link, Symbolic, Symlink Leave a comment

How to use rsync over SSH with resume support

By mariusvw
|
December 15, 2011
| No Comments
| How to

We all like backups don’t we? You know, in case your machine gets wrecked up you can restore your most precious data. With this script you can sync your data using Bash, Rsync and Open SSH. Let me first explain the doSync function. Using rsync to transfer the data is quite simple but if the […]

Read More »

Posted in How to Tagged Backup, Bash, Resume, Rsync, SSH Leave a comment

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

By mariusvw
|
July 17, 2011
| No Comments
| How to

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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/

Read More »

Posted in How to Tagged Array, Bash, in_array, Key, PHP Leave a comment

How to read a file line by line with Bash

By mariusvw
|
July 10, 2011
| No Comments
| How to

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.

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
run_cmd_file() {
    while read line
    do
        chr=${line:0:1}
        case $chr in
            "#" )  # ignore commented lines
                ;;
            *   )
                echo "$line"
                ;;
        esac
    done &lt; $1
}
 
run_cmd_file filename

Read More »

Posted in How to Tagged Bash, File, Line, Lines, Loop, Read Leave a comment

How to add bots to your Urban Terror server via RCON

By mariusvw
|
June 19, 2011
| No Comments
| FreeBSD, How to

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

1
2
/rcon bot_enable 1
/rcon reload

Next, lets add bots.

1
/rcon addbot  [level] [team] [ping] [nick]

addbot arguments type Define the name […]

Read More »

Posted in FreeBSD, How to Tagged Bash, Bots, Games, RCON, Server, Terror, Urban Leave a comment

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

By mariusvw
|
June 14, 2011
| No Comments
| How to

The following example uses tr to convert lowercase characters to UPPERcase characters.

Shell
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")

More information about tr can be found here.

Read More »

Posted in How to Tagged Bash, Case, Convert, Function, Lower, String, Upper Leave a comment

How to handle filenames containing whitespace in Bash

By mariusvw
|
June 10, 2011
| No Comments
| How to

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 😉  

Shell
1
2
3
4
5
6
7
8
#!/usr/local/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "nb")
for f in *
do
    echo "$f"
done
IFS=$SAVEIFS

 

Read More »

Posted in How to Tagged Bash, Spaces, Whitespace Leave a comment

How to trap or catch Keyboard Interrupt in Bash

By mariusvw
|
May 18, 2010
| No Comments
| How to

Sometimes you want to clean up some things when your script gets stopped for whatever reason. This script captures the SIGINT signal and sends it to the myExit function. The myExit function calls another function (just to show what is possible) which removes a logfile.

Shell
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 map the SIGINT signal to a function […]

Read More »

Posted in How to Tagged Bash, Control, Interrupt, Keyboard, Trap Leave a comment

Posts navigation

1 2 Next

Social

Search

Archives

  • December 2017
  • October 2017
  • September 2017
  • March 2017
  • February 2017
  • January 2017
  • November 2016
  • October 2016
  • February 2016
  • December 2015
  • March 2015
  • November 2013
  • July 2013
  • May 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • December 2011
  • November 2011
  • July 2011
  • June 2011
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010

Categories

  • FAQ
  • FreeBSD
  • FreeBSD world
  • Funny
  • How to
  • Projects

FreeBSD

Proud Donor

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Marius van Witzenburg 2018 | All Rights Reserved. Powered by WordPress