You are not logged in.

#1101 2010-06-09 16:42:12

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Post your handy self made command line utilities

I've had this cron'd to alert me when the provides of any manually installed package goes out of date according to the official repos.

#!/bin/bash

for package in $(pacman -Qqm); do
  pkg=($(awk '/^Version/{print $3} /^Provides/{ for(i=3;i<=NF;i++) print $i };' <(pacman -Qi $package)))

  for provide in ${pkg[@]:1}; do
    [[ $provide == "None" ]] && continue
    provide=${provide%=*}

    pacver=$(awk '/^Version/{ print $3; exit }' <(pacman -Si ${provide} 2>/dev/null))
    [[ -z ${pacver} ]] && continue

    [[ ${pacver%-*} == ${pkg[0]%-*} ]] && continue

    if [[ $(vercmp ${pacver} ${pkg[0]}) -gt 0 ]]; then
      echo -e "${provide} \033[1;31m${pkg[0]}\033[1;0m -> \033[1;32m${pacver}\033[1;0m"
    fi
  done

done

Offline

#1102 2010-06-09 17:15:20

awkwood
Member
From: .au <=> .ca
Registered: 2009-04-23
Posts: 91

Re: Post your handy self made command line utilities

jhvid wrote:
text    = ARGV[0]
command = ARGV[1]
color   = "41"   # TODO: can't get it to work with ARGV[2]

You'll have to surround your command with quotes (if it contains spaces), otherwise it will clobber ARGV[2].

jhvid wrote:
after.gsub!(" ", "_")
after.downcase!
after.gsub!(/[^a-zA-z0-9._]/, "")
after.squeeze!("_")
after.squeeze!("")

\w represents word characters [0-9A-Za-z_], and \s is for space characters [ \t\n\r\f] so this could be shortened (simplifed?) to:

after = before.gsub(/\s{1,}/, '_').gsub(/[^\w.]/, '').downcase

Offline

#1103 2010-06-09 17:49:22

jhvid
Member
From: /dev/denmark
Registered: 2009-08-19
Posts: 52

Re: Post your handy self made command line utilities

@awkwood, thanks that looks much cleaner smile


GCS d- s-:+ a12 C++++ U++++ L+++ P--- E--- W+++ N+ o K- w--- O? M-- V? PS PE
Y- PGP? t? 5? X? R tv b+++ DI+ D- G++ e++ h! !r !y

Please ignore me... I'm pretty weird...

Offline

#1104 2010-06-10 23:26:04

rwd
Member
Registered: 2009-02-08
Posts: 664

Re: Post your handy self made command line utilities

# my quick & dirty logfile check
find /var/log/* -type f -regex '[^0-9]+$'  -print0 \
 | xargs -0  egrep -nri '.*(missing|error|fail|\s(not|no .+) found|(no |not |in)valid|fatal|conflict|problem|critical|corrupt|warning|wrong|illegal|segfault|\sfault|caused|\sunable|\(EE\)|\(WW\))' \
 | more

Last edited by rwd (2010-06-10 23:36:53)

Offline

#1105 2010-06-10 23:38:02

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

Re: Post your handy self made command line utilities

rather than find -print0 | xargs -0, you could just use -exec grep -E 'whatever' {} \+

the manpage states that the \+ option to exec builds the arguments in much the same way xargs does before passing them to the exec command just once (the more ubiquitous \; execs the command once per found file).  also, i hear find -exec is the absolute most robust way to handle odd filenames.

Offline

#1106 2010-06-12 10:31:47

rwd
Member
Registered: 2009-02-08
Posts: 664

Re: Post your handy self made command line utilities

brisbin33 wrote:

rather than find -print0 | xargs -0, you could just use -exec grep -E 'whatever' {} \+

Thanks...this works indeed. Is '-print0 | xargs -0' replacable by this in all cases?

find /var/log/* -type f -regex '[^0-9]+$' \
-exec grep -Eni '.*(missing|error|fail|\s(not|no .+) found|(no |not |in)valid|\sbug|fatal|conflict|exception|problem|critical|exceed|corrupt|warning|wrong|illegal|segfault|\sfault|caused|\sunable|could not|can'\''t|cannot|\(EE\)|\(WW\))' {} \+ \
| more

Last edited by rwd (2011-06-05 10:40:11)

Offline

#1107 2010-06-12 20:08:17

ataraxia
Member
From: Pittsburgh
Registered: 2007-05-06
Posts: 1,553

Re: Post your handy self made command line utilities

I don't remember if I actually made this up myself any more, but I think I did:

alias pactesting='comm -12 <(\ls -1 /var/lib/pacman/sync/*testing | sort) <(\ls -1 /var/lib/pacman/local)'

A bash alias that shows you which packages you're using from [testing] and [community-testing].

Offline

#1108 2010-06-12 20:48:53

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Post your handy self made command line utilities

ataraxia wrote:

I don't remember if I actually made this up myself any more, but I think I did:

alias pactesting='comm -12 <(\ls -1 /var/lib/pacman/sync/*testing | sort) <(\ls -1 /var/lib/pacman/local)'

A bash alias that shows you which packages you're using from [testing] and [community-testing].

Interesting. I'd prefer to do without the ls though, and use pacman:

comm -12 <(pacman -Sl {community-,}testing | cut -d' ' -f2- | sort) <(pacman -Q)

Offline

#1109 2010-06-13 22:51:51

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

Re: Post your handy self made command line utilities

sweet guys, here's mine:

alias pactesting='pacman-color -Q $(/usr/bin/pacman -Sql {community-,}testing) 2>/dev/null'

Offline

#1110 2010-06-13 23:45:12

PReP
Member
From: Sweden
Registered: 2010-06-13
Posts: 359
Website

Re: Post your handy self made command line utilities

Here is some sort of basic xinit-exec-wm-switcher i made, after testing a lot of different similar things from aur:

http://www.pastey.net/136172

"Dependencies":

dialog,
sed,
bash tongue

#!/bin/bash 
     
    # ---- X-Session-Select Script 1.9 By P.ReP ---- 
     
    # Credits to Snosscire For friendly support :) 
    # And Gam for introducing dialog, adding the code 
    # for it, and good input for improvement :) 
     
    # ----------------------------------------------- 
     
    # You are free to do whatever with this silly, 
    # simple script. Though i would be a happy Reptile 
    # If you give a small mention if this inspired you 
    # somehow :)  /Poisionous RePTiLe 
     
    # ----------------------------------------------- 
     
     
    # -> Set The Tempfile for storing Menu-Choice 
     
    USERSSHELL="/tmp/answer$$" 
     
     
    # Run dialog, the nifty menu-thing 
     
    dialog --title "PReP X-Session Menu" \ 
           --menu "Choose Dude" 0 40 0 \ 
           "1" "Openbox" \ 
           "2" "KDE" \ 
           "3" "XBMC" 2>$USERSSHELL 
     
     
    # Return code is in $?, so.. 
     
    if [ $? != 0 ]; then 
            echo Cancelled 
            exit 0 
    fi 
     
     
    # -> Make $ANSWER equal to the value in the Tempfile 
     
    ANSWER=$(cat $USERSSHELL) 
    rm "$USERSSHELL" 
     
     
    if [ $ANSWER = 1 ]; then 
            sed 's/startkde/openbox-session/g; s/xbmc-session/openbox-session/g' /etc/X11/xinit/xinitrc > enfil 
            DIDRUN="I set stuff to openbox" 
    fi 
     
    if [ $ANSWER = 2 ]; then 
            sed 's/openbox-session/startkde/g; s/xbmc-session/startkde/g' /etc/X11/xinit/xinitrc > enfil 
            DIDRUN="I set stuff to kde!" 
    fi 
     
    if [ $ANSWER = 3 ]; then 
            sed 's/openbox-session/xbmc-session/g; s/startkde/xbmc-session/g' /etc/X11/xinit/xinitrc > enfil 
            DIDRUN="I set stuff to XBMC!" 
    fi 
     
     
    # -> Copy changes to actual xinitrc, clean up and start X 
     
    cp enfil /etc/X11/xinit/xinitrc 
    rm enfil 
     
    startx

I would probably be modified for ones own setup.
For instance the xinitrc in /etc and the specific Vm's.

I made it for having as simple a thing i could think of,
but also as a starter to learn bash/sh smile

Last edited by PReP (2010-06-13 23:46:52)


. Main: Intel Core i5 6600k @ 4.4 Ghz, 16 GB DDR4 XMP, Gefore GTX 970 (Gainward Phantom) - Arch Linux 64-Bit
. Server: Intel Core i5 2500k @ 3.9 Ghz, 8 GB DDR2-XMP RAM @ 1600 Mhz, Geforce GTX 570 (Gainward Phantom) - Arch Linux 64-Bit
. Body: Estrogen @ 90%, Testestorone @ 10% (Not scientific just out-of-my-guesstimate-brain)

Offline

#1111 2010-06-15 02:39:13

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: Post your handy self made command line utilities

I just wrote this script yesterday to rip the audio off DVDs and split it so that there is one mp3 file per chapter:

#!/bin/sh

get_chapters() {
    mplayer -identify -frames 0 -vo null -ao null -dvd-device "$DVD" dvd://"$TITLE" 2>/dev/null \
    | sed -n '/CHAPTERS: / {s///;s/,$//p}' \
    | tr -d '\n' \
    | awk 'BEGIN { RS=","; FS=":" } { print ($1*60)+$2 "." $3 }'
}

mplayer -vo null -channels 2 -srate 44100 -ao pcm:waveheader:fast:file="${FILENAME:=audiodump}".wav -dvd-device "${DVD:=/dev/dvd}" dvd://"$TITLE" -alang "${AUDIOLANG:=en}"

lame -V 6 "$FILENAME".wav "$FILENAME".mp3
rm "$FILENAME".wav

mp3splt -o @f@n "$FILENAME".mp3 `get_chapters`
rm "$FILENAME".mp3

There are no options, just pass it custom environment variables to override the defaults (which should be fairly sane). The version I use is actually slightly different than this, but I edited it a little to remove some quirky things I wanted it to do. So please tell me if I accidentally broke it.

Last edited by fflarex (2010-06-15 02:43:40)

Offline

#1112 2010-06-15 13:31:00

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Post your handy self made command line utilities

My FIRST ever Python program, so don't be too harsh. I find it to be very useful for the lazy people out there like me, and maybe useful for newbies like myself.

Edit Archlinux configuration files for system maintenance, the newbie and lazy way. Easily customize directories and the editor of your choice.

I wrote a bash script to start it with a simple command, because I am that lazy.

aaaro.png

# Archlinux Configuration editor
# by: Google, ArchLinux BBS
#!/usr/bin/python

import os,sys

os.system('clear') # Initial Clearing

####################################################
editor = 'nano'# your favorite editor goes here

## Directories for configuration files
xorg = '/etc/X11/xorg.conf'
rccon = '/etc/rc.conf'
xinitrc = '~/.xinitrc'
xlog = '/var/log/Xorg.0.log'
mkinitcpio = '/etc/mkinitcpio.conf'
modprobeconf = '/etc/modprobe.d/modprobe.conf'
resolvconf = '/etc/resolv.conf'
hosts = '/etc/hosts'
dhosts = '/etc/hosts.deny'
ahosts = '/etc/hosts.allow'
localegen = '/etc/locale.gen'
pacmancon = '/etc/pacman.conf'
pacmanmir = '/etc/pacman.d/mirrorlist'
fstab = '/etc/fstab'
####################################################

#### Function to show available options for system configuration #######
def ShowOptions():

 z = '\n                       0: EXIT          '
 a = '\n  1: edit xorg                          '
 b = '  2: edit rc.conf\n                       '
 c = '\n  3: edit xinitrc                       '
 d = '  4: view xorg log\n                      '
 e = '\n  5: edit mkinitcpio.conf               '
 f = '  6: edit modprobe.conf\n                 '
 g = '\n  7: edit resolv.conf                   '
 h = '  8: edit hosts\n                         '
 i = '\n  9: edit hosts.deny                    '
 j = ' 10: edit hosts.allow\n                  '
 k = '\n 11: edit locale.gen                   '
 l = '  12: edit pacman.conf\n                  '
 m = '\n 13: edit pacmans mirrorlist           '
 n = '  14: view fstab\n                        '

 print "***************************************************************\n"
 print "************* System Configuration and Maintenance  ***********\n"
 print "***************************************************************\n"

# Comment out what you don't need or want
 print a + b + c + d + e + f + g + h + i + j + k + l + m + n + z

####################################################
#### Function to allow user to make a choice, and not allow bad input
####
def UserInput():

  var = raw_input('\n Choose your destiny: ')

  if var == '1':
   os.system(editor + ' ' + xorg)

  if var == '2':
   os.system(editor + ' ' + rccon)

  if var == '3':
   os.system(editor + ' ' + xinitrc)

  if var == '4':
   os.system(editor + ' ' + xlog)

  if var == '5':
   os.system(editor + ' ' + mkinitcpio)

  if var == '6':
   os.system(editor + ' ' + modprobeconf)

  if var == '7':
   os.system(editor + ' ' + resolvconf)

  if var == '8':
   os.system(editor + ' ' + hosts)

  if var == '9':
   os.system(editor + ' ' + dhosts)

  if var == '10':
   os.system(editor + ' ' + ahosts)

  if var == '11':
   os.system(editor + ' ' + localegen)

  if var == '12':
   os.system(editor + ' ' + pacmancon)

  if var == '13':
   os.system(editor + ' ' + pacmanmir)

  if var == '14':
   os.system(editor + ' ' + fstab)

  if var == '0':
   os.system('clear')
   sys.exit(0)

#####################################################

#### MAIN PROGRAM ###################################

while 1 == 1:
 ShowOptions()
 UserInput()
 os.system('clear')

## END

Last edited by Google (2010-06-15 13:35:01)

Offline

#1113 2010-06-15 13:38:21

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

typo

#!/usr/bin/perl
# typo
use strict;
use LWP::UserAgent;

my $arg = shift // 'insansiate';

my $lwp = LWP::UserAgent->new(agent => 'Mozilla');
my $c = $lwp->get("http://www.google.com/search?q=$arg") or die $!;

my @content = split(/:/, $c->content);

for(@content) {
  if(m;<b><i>(.+)</i></b>;) {
    print "$1\n";
    exit;
    }
}
./typo foobaz
foo bar
./typo discnecct
disconnect

Offline

#1114 2010-06-15 14:03:53

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

Eek, huge elif chain! That's not needed! Here, I rewrote it too make it more idiomatic python and cleaned it up to match the python style guide better.

#!/usr/bin/python
#!/usr/bin/python
# Archlinux Configuration editor
# by: Google, ArchLinux BBS

import os,sys

# Get editor from env variable, or fall back to vi if not set
editor = os.environ.get('EDITOR') or os.environ.get('VISUAL') or 'vi'

## Config files
configs = {
    1: '/etc/X11/xorg.conf',
    2: '/etc/rc.conf',
    3: '~/.xinitrc',
    4: '/var/log/Xorg.0.log',
    5: '/etc/mkinitcpio.conf',
    6: '/etc/modprobe.d/modprobe.conf',
    7: '/etc/resolv.conf',
    8: '/etc/hosts',
    9: '/etc/hosts.deny',
    10: '/etc/hosts.allow',
    11: '/etc/locale.gen',
    12: '/etc/pacman.conf',
    13: '/etc/pacman.d/mirrorlist',
    14: '/etc/fstab',
}

def ShowOptions():
    """ Function to show available optionns for system configuration """
    # This function is really ugly...

    z = '\n                       0: EXIT          '
    a = '\n  1: edit xorg                          '
    b = '  2: edit rc.conf\n                       '
    c = '\n  3: edit xinitrc                       '
    d = '  4: view xorg log\n                      '
    e = '\n  5: edit mkinitcpio.conf               '
    f = '  6: edit modprobe.conf\n                 '
    g = '\n  7: edit resolv.conf                   '
    h = '  8: edit hosts\n                         '
    i = '\n  9: edit hosts.deny                    '
    j = ' 10: edit hosts.allow\n                  '
    k = '\n 11: edit locale.gen                   '
    l = '  12: edit pacman.conf\n                  '
    m = '\n 13: edit pacmans mirrorlist           '
    n = '  14: view fstab\n                        '

    print "***************************************************************\n"
    print "************* System Configuration and Maintenance  ***********\n"
    print "***************************************************************\n"

    # Comment out what you don't need or want
    print a + b + c + d + e + f + g + h + i + j + k + l + m + n + z

def UserInput():
    """ Function to allow user to make a choice, and not allow bad input """

    var = raw_input('\n Choose your destiny: ')

    if var == '0':
        os.system('clear')
        return False

    os.system(editor + ' ' + configs[var])

def main():
    os.system('clear') # Initial Clearing

    while 1:
        ShowOptions()
        UserInput() or break
        os.system('clear')

if __name__ == '__main__':
    sys.exit(main())

Last edited by Daenyth (2010-06-15 14:04:48)

Offline

#1115 2010-06-15 14:13:25

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Post your handy self made command line utilities

Thanks, I haven't learned those yet but it makes sense. I knew there must be a better way to handle the ifs, but I didn't know what else to do.  big_smile

The ShowOptions function is pretty ugly, I was going to do something like:

CheckSystem -> if config file exists, print the corresponding option -> loop through the configs and print out all of those that the system returns as 'exist'

If you get my drift... but I kind of gave up on that!  neutral

Offline

#1116 2010-06-15 14:18:54

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

Probably the easiest way would be to dynamically make the menu based on the items in the configs dict. Change the keys from numbers to labels, then iterate through the keys and display options for each one. (See 'zip()', it may help here for associating number selections to keys). Have the menu record the label of the selected choice, then use it to get the file that should be edited. There's lots of ways you can improve it, you'll get there.

I also found "dive into python" a great tutorial. That and coding things in it and finding out how things work that way. Read other people's code too. Namcap might be a good place to start, it's well written and I think there are even some feature requests open on the bug tracker wink

Last edited by Daenyth (2010-06-15 14:20:00)

Offline

#1117 2010-06-15 14:33:42

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: Post your handy self made command line utilities

Thanks, I am going to try an learn a lot. I learn mostly by doing and being corrected and messing with stuff which is why I just jumped straight into coding something.  I like the elbow deep, sink or swim approach haha. I will try reading tutorials to learn the basics more and get a good foundation.  I like the idea of being able to code something and actually use it. I learned C++ a long time ago but never found it practical in Windows or where ever. I really like what I have learned in Python and Bash so far, feels really productive and useful.

Offline

#1118 2010-06-15 14:36:10

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Post your handy self made command line utilities

I'm the same way, really. Currently working on python-networkmanager for fun and for work, so I'm having a good time smile

For bash, not to toot my own horn, but you may want to look at pkgtools. Oh and makepkg as well.

Offline

#1119 2010-06-15 14:53:59

commander_keen
Member
From: europe
Registered: 2009-12-26
Posts: 14

Re: Post your handy self made command line utilities

-

Last edited by commander_keen (2010-06-15 14:59:03)

Offline

#1120 2010-06-15 15:27:01

LinuxFreak
Member
Registered: 2010-04-17
Posts: 5

Re: Post your handy self made command line utilities

A Gtk replacement for xmessage written in Vala, that I call gtkmsg:

using Gtk;

int main( string[] args ) {
        Gtk.init( ref args );
        string title, msg;

        if( args.length <= 2 ) {
                string cmd = args[0];
                title = "gtkmsg";
                msg = @"USAGE: $cmd [TITLE] [MESSAGE]";
        }
        else {
                title = args[1];
                msg = args[2];
        }

        var msgbox = new MessageDialog( null, 0, MessageType.INFO, ButtonsType.OK, "%s", msg );
        msgbox.title = title;
        msgbox.destroy.connect( Gtk.main_quit );
        msgbox.response.connect( Gtk.main_quit );
        msgbox.show();
        Gtk.main();
        return 0;
}

Offline

#1121 2010-06-15 17:28:27

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: Post your handy self made command line utilities

@LinuxFreak: Is there something wrong with gxmessage?

Offline

#1122 2010-06-21 16:16:50

evil
Member
From: Indianapolis, IN
Registered: 2010-03-06
Posts: 41
Website

Re: Post your handy self made command line utilities

Here is what I use when batch renaming picture files

#!/usr/bin/python

import os, sys
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", "--filetype", metavar="filetype",default=".jpg", help="Choose file type to rename", type="string", dest="ren_filetype")
parser.add_option("-p", "--prefix", metavar="prefix", default="Pictures", help="Choose a filename prefix", type="string", dest="ren_prefix")
parser.add_option("-s", "--startcount", metavar="startcount", default=1, help="Choose what number to start counting from", type="int", dest="ren_startcount")
parser.add_option("-d", "--directory", metavar="directory", default=".", help="Define a different directory besides the working directory", type="string", dest="ren_directory")

(options, args) = parser.parse_args()

ren_filetype = options.ren_filetype
ren_prefix = options.ren_prefix
ren_startcount = options.ren_startcount
ren_directory = options.ren_directory

ren_list=[]
ren_filecount=0

os.chdir(ren_directory)

for a in os.listdir('.'):
    if a.endswith(ren_filetype) == True:
        ren_filecount=ren_filecount+1
    ren_list.append(a)


ren_filecount='%(#)0' + str(len(str(ren_filecount + ren_startcount))) + 'd'

for b in ren_list:
    os.rename(b, ren_prefix + '-' + (ren_filecount % {"#" : ren_startcount}) + ren_filetype)
    ren_startcount=ren_startcount+1

and this is what I use in place of cd ../../../.. etc

#!/bin/bash
x=''
for i in $(seq $1)
do
x="$x../"
done
cd $x

I named the above script to cdup and then made an alias: alias up='source cdup' so i wouldn't have to constantly prefix a . or source to the command.

This last script I use to mess with my wife when she's on the tower pc. Let me show the script first:

#!/bin/bash

run_task() {
mytype=$(cut -d ' ' -f1 /home/lucian/scripts/xcutr_script/commander)
mytask=$(cut -d ' ' -f2- /home/lucian/scripts/xcutr_script/commander)

if [ "$mytype" == "msg" ]
 then
  notify-send "Message from Lucian" "$mytask"
  echo "Sent message: $mytask at $(date +%D) $(date +%r)" >> /home/lucian/scripts/xcutr_script/comm_log
  > /home/lucian/scripts/xcutr_script/commander
elif [ "$mytype" == "exec" ]
 then
  exec $mytask &
  echo "Ran command: $mytask at $(date +%D) $(date +%r)" >> /home/lucian/scripts/xcutr_script/comm_log
  > /home/lucian/scripts/xcutr_script/commander
fi

}

while :
 do
  if [  ! $(ls -l /home/lucian/scripts/xcutr_script/commander |cut -d ' ' -f5) -eq 0 ]
   then
    run_task
  fi
done

This script is ran on start-up on my tower pc, and is constantly running. It monitors a file named "commander" in the base directory of the script for any commands. So, I just ssh in to the tower and run something like: echo "msg how are you" > commander OR echo "exec firefox www.yahoo.com" > commander

The above will either send a notify-send on the tower pc or execute firefox (or open tab if already opened) to yahoo.com

The commander file has to exist (empty) prior to running and all commands will be logged to comm_log in the base directory as well with date and time.


Site | Blog | Freenode Nick: i686

Offline

#1123 2010-06-21 16:57:58

TaylanUB
Member
Registered: 2009-09-16
Posts: 150

Re: Post your handy self made command line utilities

evil wrote:

<snip />
and this is what I use in place of cd ../../../.. etc

#!/bin/bash
x=''
for i in $(seq $1)
do
x="$x../"
done
cd $x

I named the above script to cdup and then made an alias: alias up='source cdup' so i wouldn't have to constantly prefix a . or source to the command.
<snip />

You should look into functions.

up () {
    local x=
    for i in $(seq $1)
    do
        x=$x../
    done
    cd $x
}

Remove the script and alias, and put that in your shell rc. You'll get the same behaviour.
(For "robust"ness, you might want to add a `test $# -eq 1` and `test "$1" -eq "$1" 2> /dev/null`, but it's probably overkill/useless here. tongue)


``Common sense is nothing more than a deposit of prejudices laid down by the mind before you reach eighteen.''
~ Albert Einstein

Offline

#1124 2010-06-21 17:11:14

evil
Member
From: Indianapolis, IN
Registered: 2010-03-06
Posts: 41
Website

Re: Post your handy self made command line utilities

TaylanUB wrote:
evil wrote:

<snip />
and this is what I use in place of cd ../../../.. etc

#!/bin/bash
x=''
for i in $(seq $1)
do
x="$x../"
done
cd $x

I named the above script to cdup and then made an alias: alias up='source cdup' so i wouldn't have to constantly prefix a . or source to the command.
<snip />

You should look into functions.

up () {
    local x=
    for i in $(seq $1)
    do
        x=$x../
    done
    cd $x
}

Remove the script and alias, and put that in your shell rc. You'll get the same behaviour.
(For "robust"ness, you might want to add a `test $# -eq 1` and `test "$1" -eq "$1" 2> /dev/null`, but it's probably overkill/useless here. tongue)

woot. thanks for that. i'll try that in a few.


Site | Blog | Freenode Nick: i686

Offline

#1125 2010-06-21 18:07:53

gladstone
Member
Registered: 2009-01-03
Posts: 74

Re: Post your handy self made command line utilities

You can add in the "robustness" like so:

up()
{
    for updirs in $(seq ${1:-1}); do
        cd ..
    done
}

Source.

Offline

Board footer

Powered by FluxBB