You are not logged in.

#376 2009-05-16 21:04:04

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Post your handy self made command line utilities

Some utilities for mounting stuff.

alias mta='sudo mount -a'
alias umta='sudo umount -a'
mt() {
    if [ ! -e /media/$1 ]; then
        sudo mkdir /media/$1
    fi
    sudo mount LABEL=$1 /media/$1
}
umt() {
    sudo umount /media/$1
}

Offline

#377 2009-05-18 13:13:52

Joshmotron
Member
Registered: 2009-03-04
Posts: 21

Re: Post your handy self made command line utilities

brisbin33 wrote:
Joshmotron wrote:

And command &>/dev/null & is yet another shorter and less weird way of doing that.

yeah that doesn't work for me in all cases.  good to know though.

Hmm, maybe it's just a Bash thing.. or maybe I haven't used it to its fullest extent?

http://www.tldp.org/LDP/abs/html/io-redirection.html

Offline

#378 2009-05-18 14:21:03

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

Joshmotron wrote:
brisbin33 wrote:
Joshmotron wrote:

And command &>/dev/null & is yet another shorter and less weird way of doing that.

yeah that doesn't work for me in all cases.  good to know though.

Hmm, maybe it's just a Bash thing.. or maybe I haven't used it to its fullest extent?

yeah, all the sites i've seen mention both methods as being valid (dunno about your whole 'less weird' comment... wink); i could've been typoing something, but one time i remember i had to resort to the 2>&1 at the end to get tcprobe or vobcopy to stop spitting shit to screen when i was redirecting to a file, thereafter i just 'defaulted' myself to that way.

Offline

#379 2009-05-19 13:15:13

ak-89
Member
From: Finland
Registered: 2008-08-26
Posts: 86
Website

Re: Post your handy self made command line utilities

service(){
        if [ -n "$2"  ]
        then
                sudo /etc/rc.d/$1 $2
        else
                echo    "All daemons"
                ls --ignore=functions* /etc/rc.d/
                echo    "Running daemons"
                ls /var/run/daemons
        fi
}
start(){
        service $1 start
}
stop(){
        service $1 stop
}
restart(){
        service $1 restart
}

This to .bashrc. If no arguments given, it shows what daemon scripts you have in /etc/rd.d/ and running daemons and as you can see it starts, stops and restarts daemons.

Offline

#380 2009-05-19 14:35:57

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: Post your handy self made command line utilities

hi ak-89,

Here is the function I'm using as well as the bash completion file I use:

# grep service .bashrc 
service () { /etc/rc.d/$1 $2 ; }     #alias 'service' to /etc/rc.d
# cat /etc/bash_completion.d/service 
_service_complete()
{
  local cur prev

  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}

  if [ "$prev" == service ]; then
    COMPREPLY=( $(compgen -W "\
      $(  for service in /etc/rc.d/*; do
            echo ${service##*/}
          done )" -- $cur ) )
  else
    COMPREPLY=( $(compgen -W '\
      start
      stop
      restart
      reload' -- $cur ) )
  fi
}

complete -F _service_complete servic

Offline

#381 2009-05-19 18:06:51

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

Upload your image to google picasa web album.
You can choose which album to upload to, and get the link for image and thumbnails.
Enjoy.^^

#!/bin/bash
# Usage $0 /path/to/file [PicName]

my_pic="$1"
[ -z "$2" ] &&
pic_title=`basename "$my_pic"` ||
pic_title="$2"
pic_type=`file -b --mime-type "$my_pic"`

### Your google account and password
username=
password=

auth_key=$( curl -s https://www.google.com/accounts/ClientLogin -d Email="$username"@gmail.com -d Passwd="$password" -d accountType=GOOGLE -d source=Google-cURL-Example -d service=lh2 | awk -F\= '/^Auth=/{print $2}' )

picasa_default_out="picasa_default.tmp"
picasa_album_addrs="picasa_album_addrs.tmp"
picasa_album_names="picasa_album_names.tmp"

curl -s --header "Authorization: GoogleLogin auth=$auth_key" "http://picasaweb.google.com/data/feed/api/user/default" > "$picasa_default_out"

echo "**** Album Addrs Are ****"
cat "$picasa_default_out" | tr '>' '\n' | grep "/feed/api/user/" | grep '<link rel' | grep id | cut -d\' -f 6 | tee "$picasa_album_addrs"
echo "**** Album Names Are ****"
idx=1
cat "$picasa_default_out" | tr '>' '\n' | grep "</gphoto:name" | cut -d'<' -f 1 | tee "$picasa_album_names" | while read line ; do echo "$idx"') '"$line" ; (( idx++ )) ; done

echo "**** Choose Up-Album ****"
echo ":"
read my_choice

album_name=$( head -"$my_choice" "$picasa_album_names" | tail -1 )
album_addr=$( head -"$my_choice" "$picasa_album_addrs" | tail -1 )

echo "**** Uploading $pic_title to $album_name ****"

# album_xml=$( curl -s --header "Authorization: GoogleLogin auth=$auth_key" "http://picasaweb.google.com/data/feed/api/user/default" | sed 's/.*<link rel='"'"'self'"'"' type='"'"'application\/atom+xml'"'"' href='"'"'\([^'"'"']*\).*/\1/' | sed 's/entry/feed/' )

my_pic_info="picasa_pic_info.tmp"
my_pic_info_brief="picasa_pic_info_brief.tmp"

curl -# --request POST --data-binary "@$my_pic" --header "Slug: $pic_title" --header "Content-Type: $pic_type" --header "Authorization: GoogleLogin auth=$auth_key" "$album_addr" > "$my_pic_info"

cat "$my_pic_info" | tr '>' '\n' | sed '/media:content url/,$!d' > "$my_pic_info_brief"

my_pic_page_url=$( cat "$my_pic_info" | tr '>' '\n' | grep 'text/html' | grep "$album_name" | cut -d\' -f 6 )
my_pic_src_url=$( cat "$my_pic_info_brief" | head -1 | cut -d\' -f 2  )
my_pic_s72_url=$( cat "$my_pic_info_brief" | grep '/s72/' | cut -d\' -f 2 )
my_pic_s144_url=$( cat "$my_pic_info_brief" | grep '/s144/' | cut -d\' -f 2 )
my_pic_s288_url=$( cat "$my_pic_info_brief" | grep '/s288/' | cut -d\' -f 2 )

echo "**** Image SourceUrl ****"
echo "$my_pic_src_url"
echo "**** Image ThumbUrls ****"
echo "$my_pic_s72_url"
echo "$my_pic_s144_url"
echo "$my_pic_s288_url"
echo "**** Album View Page ****"
echo "$my_pic_page_url"

Sample Output::

**** Album Addrs Are ****
http://picasaweb.google.com/data/feed/a … b_3ab7xpNR
http://picasaweb.google.com/data/feed/a … 7224438801
**** Album Names Are ****
1) My Private Album 1 Which Is Porn
2) TravelInArctic
**** Choose Up-Album ****
:
2
**** Uploading Unix_Test to TravelInArctic ****
######################################################################## 100.0%
**** Image SourceUrl ****
http://lh4.ggpht.com/_JoAtMSTeu1g/ShX_2 … x_Test.png
**** Image ThumbUrls ****
http://lh4.ggpht.com/_JoAtMSTeu1g/ShX_2 … x_Test.png
http://lh4.ggpht.com/_JoAtMSTeu1g/ShX_2 … x_Test.png
http://lh4.ggpht.com/_JoAtMSTeu1g/ShX_2 … x_Test.png
**** Album View Page ****
http://picasaweb.google.com/your_name/T … 6424819746

Edit:: According to your account type, you may need to modify "@gmail.com" to "@googlemail.com".
         Thanks for that one, markp1989;)
Edit:: Edited code, made it better.
Note:: Those picasa_*.tmp files contain infos of your upload, may contain auth keys to your private *Porn* Album, be careful

Last edited by lolilolicon (2009-05-22 05:26:33)


This silver ladybug at line 28...

Offline

#382 2009-05-19 21:20:13

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Post your handy self made command line utilities

lolilolicon wrote:

Upload your image to google picasa web album.
Plus, get the link for image and thumbnail.
Enjoy.^^

#!/bin/bash
#returns source image location and thumbnail location
#Usage: picasa.sh  /path/to/pic  [Pic Name Shown In Album]

my_pic="$1"
[ -z "$2" ] &&
pic_title=`basename $"my_pic"` ||
pic_title="$2"
pic_type=`file -b --mime-type "$my_pic"`

#Your google account and password
username=
password=

auth_key=$( curl -s https://www.google.com/accounts/ClientLogin -d Email="$username"@gmail.com -d Passwd="$password" -d accountType=GOOGLE -d source=Google-cURL-Example -d service=lh2 | awk -F\= '/^Auth=/{print $2}' )

album_xml=$( curl -s --header "Authorization: GoogleLogin auth=$auth_key" "http://picasaweb.google.com/data/feed/api/user/default" | sed 's/.*<link rel='"'"'self'"'"' type='"'"'application\/atom+xml'"'"' href='"'"'\([^'"'"']*\).*/\1/' | sed 's/entry/feed/' )

curl -s --request POST --data-binary "@$my_pic" --header "Slug: $pic_title" --header "Content-Type: $pic_type" --header "Authorization: GoogleLogin auth=$auth_key" "$album_xml" |
sed 's/.*media:content url='"'"'\([^'"'"']*\).*media:thumbnail url='"'"'\([^'"'"']*\).*/\1\n\2/'

thats prety cool, and works fine big_smile  i had to change the the "gmail.com" to "googlemail.com" becuase thats how my email address works, thanks


Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#383 2009-05-22 03:54:33

kandrews
Member
Registered: 2007-04-21
Posts: 119

Re: Post your handy self made command line utilities

Here's how I connect to my netcfg profiles in wmii.

#!/bin/bash
# wmii netcfg manager

# get current wmii color scheme
wmiictl='wmiir read ctl' 
font=`$wmiictl | grep font | sed 's/font //'`
norm=`$wmiictl | grep norm | sed 's/normcolors //'`
sel=`$wmiictl | grep focus | sed 's/focuscolors //'`
norm_bgcolors=`echo $norm | cut -d ' ' -f 2`
sel_bgcolors=`echo $sel | cut -d ' ' -f 2`
norm_fgcolors=`echo $norm | cut -d ' ' -f 1`
sel_fgcolors=`echo $sel | cut -d ' ' -f 1`

# get configured profiles
profiles=`ls /etc/network.d/ -1Ap \
    | grep -v /\$` 

# additional dmenu options can be set here
choice=`echo "$profiles" | dmenu -i -b \
    -fn "$font" \
    -nf "$norm_fgcolors" \
    -nb "$norm_bgcolors" \
    -sf "$sel_fgcolors" \
    -sb "$sel_bgcolors"`

# make sure the selection exists
for i in $profiles; do
    if [[ $choice = $i ]]; then 
        sudo netcfg -a 
        sudo netcfg $choice
    fi
done

This is my first 3< line  bash script, so I welcome suggestions to improve it!

edit: here is my shutdown dmenu script

#!/bin/bash
# wmii shutdown manager

# get current wmii color scheme
wmiictl='wmiir read ctl' 
font=`$wmiictl | grep font | sed 's/font //'`
norm=`$wmiictl | grep norm | sed 's/normcolors //'`
sel=`$wmiictl | grep focus | sed 's/focuscolors //'`
norm_bgcolors=`echo $norm | cut -d ' ' -f 2`
sel_bgcolors=`echo $sel | cut -d ' ' -f 2`
norm_fgcolors=`echo $norm | cut -d ' ' -f 1`
sel_fgcolors=`echo $sel | cut -d ' ' -f 1`

# menu items
items='halt\nreboot\nsuspend\nhibernate'
# process items and display menu
choice=`echo -e "$items" | dmenu -i -b \
    -fn "$font" \
    -nf "$norm_fgcolors" \
    -nb "$norm_bgcolors" \
    -sf "$sel_fgcolors" \
    -sb "$sel_bgcolors"`

# "logic"
case $choice in
    halt) exec sudo halt;;
    reboot) exec sudo reboot;;
    suspend) exec sudo pm-suspend;;
    hibernate) exec sudo pm-hibernate;;
    *) exit 1 # error
esac

# it worked!
exit 0

Last edited by kandrews (2009-05-22 04:41:50)

Offline

#384 2009-05-22 05:16:33

Wintervenom
Member
Registered: 2008-08-20
Posts: 1,011

Re: Post your handy self made command line utilities

Post deleted.

Last edited by Wintervenom (2009-08-03 13:22:56)

Offline

#385 2009-05-23 01:45:35

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Post your handy self made command line utilities

Small function to install stuff from ABS.

absi() {
    { test -z "$1" || test -z "$2" ;} && {
        echo "Usage: absi <repo> <pkg>"
        return 1
    }
    ( mkdir -p ~/.absi-stage/$1/$2 &&
        cp -r /var/abs/$1/$2/* ~/.absi-stage/$1/$2/ &&
        cd ~/.absi-stage/$1/$2 &&
        makepkg -i )
}

# Note: I'm using parentheses for the second part because that way, it doesn't affect the main shell's working directory.

Last edited by Peasantoid (2009-05-23 01:46:45)

Offline

#386 2009-05-23 10:14:15

abesto
Member
From: Hungary
Registered: 2009-03-05
Posts: 49
Website

Re: Post your handy self made command line utilities

A little script to start/stop httpd and mysqld, and optionally open/close port 80 if you're using firehol, plus update your DynDNS IP. I use it when I do web development locally. The part that opens port 80 assumes that you have a commented "server http accept" in /etc/firehol/firehol.conf

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

DYNDNS=1  # Use DynDNS?
FIREHOL=1 # Use firehol?

case "$1" in
    start|restart)
        if [ $DYNDNS -eq 1 ]; then
            stat_busy "Updating DynDNS IP"
            dyndns.sh > /dev/null 2>&1
            stat_done #TODO: check if update was succesful
        fi
        if [ $FIREHOL -eq 1 ]; then
            stat_busy "Opening HTTP port"
            sed -e 's/^[ \t]*#[ \t]*server http accept/\    server http accept/' /etc/firehol/firehol.conf > /tmp/firehol.conf
            mv /tmp/firehol.conf /etc/firehol/firehol.conf
            /etc/rc.d/firehol start > /dev/null 2>&1
            stat_done
        fi
        ;;
    stop)
        if [ $DYNDNS -eq 1 ]; then
            stat_busy "Updating DynDNS IP to 127.0.0.1"
            dyndns.sh 127.0.0.1 > /dev/null 2>&1
            stat_done
        fi
        if [ $FIREHOL -eq 1 ]; then
            stat_busy "Closing HTTP port"
            sed -e 's/^[ \t]*server http accept/\    # server http accept/' /etc/firehol/firehol.conf > /tmp/firehol.conf
            mv /tmp/firehol.conf /etc/firehol/firehol.conf
            /etc/rc.d/firehol start > /dev/null 2>&1
            stat_done
        fi
        ;;
    *)
        echo "Usage: $0 {start|restart|stop}"
        exit 1;
        ;;
esac

/etc/rc.d/httpd $1
/etc/rc.d/mysqld $1

And the dyndns.sh used (most of it isn't mine, but I can't seem to find the original source). It has to be in your $PATH

#!/bin/sh
# This script uses "dyndnsupdate" available at http://www.bebits.com/app/2927

HOSTNAME=DYNDNS_HOSTNAME
NSLOOKUP=`/usr/bin/nslookup -sil $HOSTNAME | tail -2 | head -1 | cut -d" " -f2`

case "$1" in
    '')
        CURRENT_IP=`/usr/bin/lynx -dump http://www.netins.net/dialup/tools/my_ip.shtml | grep -A2 "Your current IP Address is:" | tail -n1 | tr -d ' '`
        if [ "$CURRENT_IP" = "unknown" ] ; then
            exit
        fi
        ;;
    *)
        CURRENT_IP=$1
        ;;
esac

# Is our current IP in the DynDNS DNS records? If not, update them.
if [ "$NSLOOKUP" != "$CURRENT_IP" ] ; then
   /sbin/dyndnsupdate -a $CURRENT_IP -h $HOSTNAME -m \
   $HOSTNAME\'s_mail_exchange.dyndns.org -s statdns \
   -u DYNDNS_USERNAME:DYNDNS_PASSWORD
fi

Linux user #476135 || Dotfiles hosted by GitHub

Offline

#387 2009-05-26 08:13:02

Jaejae
Member
Registered: 2008-05-17
Posts: 80

Re: Post your handy self made command line utilities

Based on a script I saw somewhere, but never read, perhaps even in this thread, but entirely original code.

I know it probably isn't the most efficient way of going about it, but I'm still learning, and for all means and purposes, it works for me.

EDIT: Updated

First script saves the currently playing track to a list, second one selects a random track from that list and plays it.

It will probably break if your list contains one entry, and you try and invoke the second script while listening to that track (eval `$0` loop)

#!/bin/sh

gtrack=$(mpc | head -1)

if [ -e $HOME/.gslist ]
then
        touch $HOME/.gslist
fi

if [ "$(cat "$HOME/.gslist" | grep "^$gtrack$")" != "$gtrack" ]
then
        echo $gtrack >> $HOME/.gslist
        echo -e "Track added to gslist\007"
else
        echo -en "ERROR: Track is present in gslist\007"
        sleep 0.2
        echo -e "\007"
fi
#!/bin/sh

if [ -e "$HOME/.gslist" ]
then
        gline=$(wc -l "$HOME/.gslist" | awk '{print $1}')
        rand=$[ ( $RANDOM % $gline ) + 1 ]
        line=$(awk "FNR == $rand" "$HOME/.gslist")
        if [ "$line" != "$(mpc | head -1)" ]
        then
                num=$(mpc playlist | grep "$line" | awk '{print $1}' | sed 's/ |)//')
                mpc play $num
        else
                eval `$0`
        fi
else
        echo "ERROR: $HOME/.gslist is not present, add some songs"
fi

Last edited by Jaejae (2009-05-27 03:07:44)

Offline

#388 2009-05-28 14:05:12

LeafStorm
Member
From: North Carolina
Registered: 2009-05-20
Posts: 51
Website

Re: Post your handy self made command line utilities

I wrote a weather script in Lua for my Conky. It's pretty much the first real script I've written in Lua, but it works well. The Lua version of this script can run in about .150 seconds, while a nearly identical Python version takes .250-.300. It uses Yahoo's weather API since it's not sending as much downstream and the RSS feed is easy to parse with a pattern. (Requires LuaSocket.)

#!/usr/bin/env lua
-- Lua weather downloader
-- Gets info from Yahoo! Weather API

require 'socket.http'

baseurl = 'http://weather.yahooapis.com/forecastrss?u=XYZ&p='

if #arg < 1 then
    print("Please specify a ZIP code or Yahoo location ID")
    os.exit(1)
end

zipcode = arg[1]

if #arg > 1 then
    if string.lower(arg[2]) == 'f' or string.lower(arg[2]) == 'c' then
        units = string.lower(arg[2])
    else
        units = 'f'
    end
else
    units = 'f'
end

pattern = [=[<yweather:condition%s+text=['"]([%s%w]+)['"].+temp=['"](%d+)['"]]=]

url = string.gsub(baseurl, 'XYZ', units) .. zipcode

weatherdata = socket.http.request(url)

condition, temp = string.match(weatherdata, pattern)

print(condition .. ", " .. tostring(temp) .. string.upper(units))

Thanks,
Matthew Frazier

Offline

#389 2009-05-30 19:23:26

m1sha
Member
Registered: 2009-04-23
Posts: 9

Re: Post your handy self made command line utilities

BASE=$HOME"/etk/"
DATE=`date +%Y%j
STORE=$BASE$DATE
YEST=$BASE$[$DATE-1]

mkdir -pv $STORE

cp -pvu --target-directory=$STORE \
    /etc/X11/xorg.conf \
    /etc/pacman.conf \
    /etc/rc.conf \
    /boot/grub/menu.lst

echo $BASE$[$DATE-1].7z $YEST
7z a $BASE$[$DATE-1].7z $YEST
rm $YEST -rvf


echo "Nice one!"

Backs up the config files I regularly break, does something odd on new year's day.

Offline

#390 2009-05-31 03:09:17

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

so, i found myself in need of a few awk printf strings this week.  i decided to write a little script to a) reinforce the syntax and b) quickly remind me when i need one of these awk lines some time in the future.  i hope to add more as i need/learn them.

NOTE: what's below is NOT the script; it is the script's output. wink

┌─[ 22:58 ][ blue:~ ]
└─> manawk 

#!/bin/bash
# an illustrative script for awk printing syntax
###
echo ""
head -n24 $0

### COMMANDS
#
echo 123456 1234567890123456 | awk '{printf "| %10s | %10s |\n", $1, $2}'
echo 123456 1234567890123456 | awk '{printf "| %-10s | %-10s |\n", $1, $2}'
echo ""
echo 123456 1234567890123456 | awk '{printf "| %10.10s | %10.10s |\n", $1, $2}'
echo 123456 1234567890123456 | awk '{printf "| %-10.10s | %-10.10s |\n", $1, $2}'
echo ""
echo 123456 1234567890123456 | awk '{printf "| %10d | %10d |\n", $1, $2}'
echo 123456 1234567890123456 | awk '{printf "| %-10d | %-10d |\n", $1, $2}'
echo ""
echo 123456 1234567890123456 | awk '{printf "| %10.10d | %10.10d |\n", $1, $2}'
echo 123456 1234567890123456 | awk '{printf "| %-10.10d | %-10.10d |\n", $1, $2}'
#
### /COMMANDS

### OUTPUT
#
|     123456 | 1234567890123456 |
| 123456     | 1234567890123456 |

|     123456 | 1234567890 |
| 123456     | 1234567890 |

|     123456 | 1234567890123456 |
| 123456     | 1234567890123456 |

| 0000123456 | 1234567890123456 |
| 0000123456 | 1234567890123456 |
#
### /OUTPUT

### NOTE:
#
#   %-N*  switches alignment
#   %N.Ns restrains overflow
#   %N.Nd pads with zeros
#
### /NOTE
┌─[ 22:58 ][ blue:~ ]
└─>

Offline

#391 2009-05-31 04:29:41

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

Nice!
BTW, how do you get this?

| 123456     | 7890123456 |


This silver ladybug at line 28...

Offline

#392 2009-05-31 04:37:14

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

m1sha wrote:

Backs up the config files I regularly break, does something odd on new year's day.

from man 7z::

DO NOT USE the 7-zip format for backup purpose on Linux/Unix because :
        - 7-zip does not store the owner/group of the file.

       On Linux/Unix, in order to backup directories you must use tar :
        - to backup a directory  : tar cf - directory | 7za a -si directory.tar.7z
        - to restore your backup : 7za x -so directory.tar.7z | tar xf -


This silver ladybug at line 28...

Offline

#393 2009-05-31 04:44:26

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

lolilolicon, i actually would've expected that to happen on overflow with one of the N.Ns's but as you can see it didn't, and i'm afraid the man pages are a bit greek to me...

maybe when you find out you can let me know so i can add it smile

Offline

#394 2009-05-31 04:47:46

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Post your handy self made command line utilities

I was expecting that too... strange. I think Procyon will come and tell us how to do that :-)


This silver ladybug at line 28...

Offline

#395 2009-05-31 04:50:37

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Post your handy self made command line utilities

HA!

you're so right... he's like the Xyne of bash...

Offline

#396 2009-05-31 09:28:01

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Post your handy self made command line utilities

ak-89 wrote:
service(){
        if [ -n "$2"  ]
        then
                sudo /etc/rc.d/$1 $2
        else
                echo    "All daemons"
                ls --ignore=functions* /etc/rc.d/
                echo    "Running daemons"
                ls /var/run/daemons
        fi
}
start(){
        service $1 start
}
stop(){
        service $1 stop
}
restart(){
        service $1 restart
}

This to .bashrc. If no arguments given, it shows what daemon scripts you have in /etc/rd.d/ and running daemons and as you can see it starts, stops and restarts daemons.

Oh hoo!  Very nice.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#397 2009-05-31 11:41:14

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Post your handy self made command line utilities

lolilolicon wrote:

Nice!
BTW, how do you get this?

| 123456     | 7890123456 |

substr can do that:
$ echo 123456 1234567890123456789 | awk '{printf "| %-10.10s | %-10.10s |", $1, substr($2,length($2)-9)}'
| 123456     | 0123456789 |

Offline

#398 2009-05-31 19:55:24

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Post your handy self made command line utilities

#!/bin/bash
test "$(whoami)" != "root" && {
    echo "Must be run as root."
    exit 1
}
bckdir=/media/Arch_Backups
bcknm="arch-$(date +'%d.%m.%y')"
echo "Backing up system..."
if ! mountpoint -q "$bckdir"; then
    echo "\"$bckdir\" not mounted."
    exit 1
elif test -e "$bckdir/$bcknm"; then
    echo "\"$bckdir/$bcknm\" already exists."
    exit 1
fi
cp -avx / "$bckdir/$bcknm"

Random little backup script I wrote a week or two ago. It'll only work if the destination directory is a mounted drive, due to some cp limitation or other.

# Oops, forgot to say what it is... roll

Last edited by Peasantoid (2009-05-31 20:04:16)

Offline

#399 2009-05-31 21:55:14

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Post your handy self made command line utilities

It'll only work if the destination directory is a mounted drive, due to some cp limitation or other.

Umm, I don't think I get it. If you want to backup everything (/dev? /proc?) and copy it to f.e. /home/backup, which itself will be also backed up to /home/backup ... 'cp -x' means "stay on this file system" so if you mount the bckdir it won't get copied in the loop-like fashion (haven't tested it, but that's what I infer from man pages).

Offline

#400 2009-05-31 22:00:23

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Post your handy self made command line utilities

You can't copy a directory into a subdirectory of itself unless the subdirectory is mounted. Allow me to show you what I mean:

peasantoid # cp -avx / /media/backups/arch-31.05.09
`/' -> `/media/backups/arch-31.05.09'
`/media' -> `/media/backups/arch-31.05.09/media'
`/media/cd' -> `/media/backups/arch-31.05.09/media/cd'
`/media/.hal-mtab' -> `/media/backups/arch-31.05.09/media/.hal-mtab'
`/media/dvd' -> `/media/backups/arch-31.05.09/media/dvd'
`/media/backups' -> `/media/backups/arch-31.05.09/media/backups'
cp: cannot copy a directory, `/', into itself, `/media/backups/arch-31.05.09'

(In this example, /media/backups is not mounted.)

Last edited by Peasantoid (2009-05-31 22:04:59)

Offline

Board footer

Powered by FluxBB