View the Most Wanted LQ Wiki articles.
LinuxQuestions.org > Linux Wiki > Scripts

From LQWiki

(Redirected from SCRIPTS)
Jump to: navigation, search

Scripts are small programs that are not compiled, but interpreted. They are often written in languages such as Perl, PHP, JavaScript. See here for a list of interpreted languages.

If not specified otherwise, a script is usually written in bash.

Contents

Bash scripting

Main article: bash tips

Hello world

The minimal bash script that only outputs "hello world" looks like this:

#!/bin/bash
echo "hello world"

Find out your distribution

Main article: find out your distribution

The following script tells you what distribution you have installed on your computer.

#!/bin/bash
found=0;
if [ -e /etc/SuSE-release ]; then echo "You have a SUSE distro"; export found=1; fi
if [ -e /etc/redhat-release ]; then echo "You have a Red Hat distro"; export found=1; fi
if [ -e /etc/fedora-release ]; then echo "You have a Fedora distro"; export found=1; fi
if [ -e /etc/debian-version ]; then echo "You have a Debian, Ubuntu, Kubuntu, Edubuntu or Flubuntu distro"; export found=1; fi
if [ -e /etc/slackware-version ]; then echo "You have a SlackWare distro"; export found=1; fi
if ! [ $found = 1 ]; then echo "I could not find out your distro"; fi

It looks if the respective files exist (if [ -e /etc/SuSE-release ];) and prints the distribution they flag (using the command echo).

Tonka Script

Changes your console to some other colours.

#!/bin/bash

function tonka {

#   Named "Tonka" because of the colour scheme

local WHITE="\[\033[1;37m\]"
local LIGHT_BLUE="\[\033[1;34m\]"
local YELLOW="\[\033[1;33m\]"
local NO_COLOUR="\[\033[0m\]"

case $TERM in
    xterm*|rxvt*)
        TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)
        TITLEBAR=""
        ;;
esac

PS1="$TITLEBAR\
$YELLOW-$LIGHT_BLUE-(\
$YELLOW\u$LIGHT_BLUE@$YELLOW\h\
$LIGHT_BLUE)-(\
$YELLOW\$PWD\
$LIGHT_BLUE)-$YELLOW-\
\n\
$YELLOW-$LIGHT_BLUE-(\
$YELLOW\$(date +%H%M)$LIGHT_BLUE:$YELLOW\$(date \"+%a,%d %b %y\")\
$LIGHT_BLUE:$WHITE\\$ $LIGHT_BLUE)-$YELLOW-$NO_COLOUR "

PS2="$LIGHT_BLUE-$YELLOW-$YELLOW-$NO_COLOUR "

}

Get your ip

#!/bin/bash
# get ip
/sbin/ifconfig $1 | grep inet | awk '{print $2}' | sed 's/^addr://g'

To get your Internet address if you are behind a NAT:

## The -n option retrieves the Internet IP address
## if you are behind a NAT
if [ "$1" = "-n" ]
then
  ip=$(lynx -dump http://cfaj.freeshell.org/ipaddr.cgi)
else
  if=$1   ## specify which interface, e.g. eth0, fxp0
  system=$(uname)
  case $system in
      FreeBSD) sep="inet " ;;
      Linux) sep="addr:" ;;
  esac
  temp=$(ifconfig $if)
  temp=${temp#*"$sep"}
  ip=${temp%% *}
fi

printf "%s\n" "$ip"
### CFAJ ###

See also


Personal tools