SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2010-08-20 17:51:16

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

shell script bits & pieces thread

I wondered if it might be worth having a thread for people to post bits of code they wanted to share, but which didn't seem worth starting a whole new thread for. Let's keep this one for shell stuff - bash and dash - though; maybe python, perl, lua... should each have their own thread.

Anyway, to start off, here's a line that's handy to put in the top of a script if you want to make sure it's run in a terminal. For those scripts that need user interaction:

[ -t 0 ] && [ -t 1 ] || { echo "${0}: this script must be run from a terminal" ; exit 1 ;}

This tests if stdin and stdout are connected to a terminal, and exits with an error message if they're not. (So where does the error message go? To ~/.xsession-errors with any luck.)

Last edited by johnraff (2010-08-23 16:36:03)

Offline

Be excellent to each other!

#2 2010-08-21 23:53:29

~HP
#! CrunchBanger
From: Elsaß (fr_FR)
Registered: 2010-01-01
Posts: 187
Website

Re: shell script bits & pieces thread

A solution is to use logger :

log() {
    echo "$1" 1>&2 
    logger -ist "$(basename -- "$0")" "$1"
}

piece of code used in my autostart.sh

Offline

#3 2010-08-23 16:41:40

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

^where do you get logger? It's not in the Jaunty repositories.

Offline

#4 2010-08-23 18:02:18

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

#!/bin/bash
# words.sh
# counts words in selection and displays result in zenity window
# dependencies xsel, wc, zenity

text=$(xsel)
words=$(wc -w <<<$text)
zenity --info --title "Word Count" --text "Words in selection:\n${words}\n\n\"${text}\""
exit

Offline

#5 2010-08-23 20:49:31

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,419

Re: shell script bits & pieces thread

Offline

#6 2010-08-24 04:59:32

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

^ah I should have just tried 'man logger' before looking for the package roll
That util-linux package has some stuff I use often and a lot I've never heard of but maybe should be using so thanks for that.
logger itself turned out to be in bsdutils, at least on #! 9.04.

Offline

#7 2010-08-24 05:09:55

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

cmdpkg(){ PACKAGE=$(dpkg -S $(which $1) | cut -d':' -f1); echo "[${PACKAGE}]"; dpkg -s "${PACKAGE}" ;}

Offline

#8 2010-08-24 07:29:55

murraythegoz
#! Member
From: Milan Area, Italy
Registered: 2009-04-29
Posts: 77

Re: shell script bits & pieces thread

dos2unix, the perl way

perl -pi -e 's/\r\n/\n/g' *

check for IP address (found somewhere in activestate forums)

(?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)(?:\.(?:1\d?\d?|2(?:[0-4]\d?|[6789]|5[0-5]?)?|[3-9]\d?|0)){3}

!# statler 10 + eee-control running on eee701/4gb/512ram
!# statler/sid running on MacBookAir 4,1 (mid 2011) i7-1.8GHz/128SSD/4g RAM

Offline

#9 2010-10-27 16:41:47

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

I've got a lot of distro-independent stuff on a separate partition, mounted as /data, with symlinks from my home directory so ~/documents is really a symlink to /data/john/documents. If you want to refer to a file or directory in a script it often doesn't matter how you get there, via the symlink or directly, but sometimes it does matter.

You can get the real location of a file with the command 'readlink -f' so, for example

john@raffles3:~$ readlink -f documents
/data/john/documents

Comes in handy sometimes.

Offline

#10 2010-12-02 11:33:44

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

'What's this?'

Offline

#11 2010-12-05 01:36:42

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,419

Re: shell script bits & pieces thread

#!/bin/sh
echo '========================>>> fsarchiver probe simple <<<========================'
sudo fsarchiver probe simple
echo ' '
echo 'Press a key to continue'
read key

Offline

#12 2010-12-07 04:55:34

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

awk '!a[$0]++' file.txt

Offline

#13 2010-12-07 05:47:32

Chewie
#! CrunchBanger
From: /dev/null
Registered: 2010-12-01
Posts: 103

Re: shell script bits & pieces thread

#!/bin/sh
echo '========================>>> fsarchiver probe simple <<<========================'
sudo fsarchiver probe simple
echo ' '
echo 'Press a key to continue'
read key

Offline

#14 2010-12-08 17:07:21

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

^looks good but fsarchiver isn't in the Jaunty repos. I really have to make the shift to Statler here soon - starting to feel left behind. neutral

Offline

#15 2010-12-09 01:09:34

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,419

Re: shell script bits & pieces thread

Well on the bright side what better time to switch to Statler than now with the newly released images.

Offline

#16 2010-12-09 04:23:45

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

Yes indeed - time to nuke my defunct Xp partition and put it there. cool

Offline

#17 2010-12-22 08:22:02

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 597

Re: shell script bits & pieces thread

i have this in my ~/.bash_aliases

comp () { #compare the speed of two commands (loop $1 times)
  if [[ $# -ne 3 ]] ; then return 1 ; fi
  echo -n 1
  time for ((i=0;i<$1;i++)) ; do $2 ; done >/dev/null 2>&1
  echo -n 2
  time for ((i=0;i<$1;i++)) ; do $3 ; done >/dev/null 2>&1
}

and now if I want to see which of two commands is faster i type

comp 100 "command_1 args" "command_2 args"

take care to quote corectly so that "command_1 args" will end up as "$2" etc. (i often define little functions f1, f2 interactively to test them against each other)

luc

Offline

#18 2010-12-22 11:51:01

Sector11
#!'er to BL'er
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 15,667
Website

Re: shell script bits & pieces thread

[ -t 0 ] && [ -t 1 ] || { echo "${0}: this script must be run from a terminal" ; exit 1 ;}

Offline

#19 2010-12-22 13:12:16

mobilediesel
#! Member
Registered: 2010-08-19
Posts: 73

Re: shell script bits & pieces thread

[ -t 0 ] && [ -t 1 ] || { echo "${0}: this script must be run from a terminal" ; exit 1 ;}

Offline

#20 2010-12-22 15:06:13

Sector11
#!'er to BL'er
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 15,667
Website

Re: shell script bits & pieces thread

#!/bin/sh
echo '========================>>> fsarchiver probe simple <<<========================'
sudo fsarchiver probe simple
echo ' '
echo 'Press a key to continue'
read key

Last edited by Sector11 (2010-12-22 15:07:28)

Offline

#21 2010-12-22 15:12:34

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,419

Re: shell script bits & pieces thread

Its because for some reason I setup sudo to not require a password for anything. I have now fixed my sudoers file so thank you for pointing that out to me.

Offline

#22 2010-12-22 15:45:42

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: shell script bits & pieces thread

comp () { #compare the speed of two commands (loop $1 times)
  if [[ $# -ne 3 ]] ; then return 1 ; fi
  echo -n 1
  time for ((i=0;i<$1;i++)) ; do $2 ; done >/dev/null 2>&1
  echo -n 2
  time for ((i=0;i<$1;i++)) ; do $3 ; done >/dev/null 2>&1
}

Offline

#23 2010-12-22 15:57:35

Sector11
#!'er to BL'er
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 15,667
Website

Re: shell script bits & pieces thread

#!/bin/sh
sudo fsarchiver probe simple
echo ' '
echo '========================>>> fsarchiver probe simple <<<========================'
echo ' '
echo 'Press a key to continue'
read key

Last edited by Sector11 (2010-12-22 15:59:45)

Offline

#24 2010-12-22 16:02:40

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,419

Re: shell script bits & pieces thread

You can modify the script however you like. Also in your version, both echo ' ' are working fine. After fsarchiver, you can see two blank lines.

Offline

Be excellent to each other!

#25 2010-12-22 16:16:30

Sector11
#!'er to BL'er
From: SR11 Cockpit
Registered: 2010-05-05
Posts: 15,667
Website

Re: shell script bits & pieces thread

[ -t 0 ] && [ -t 1 ] || { zenity --warning --text="${0}: this script must be run from a terminal." ; exit 1 ;}

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: bleh

Debian Logo