Useful scripts

grepe, short for "grep edit":
Code:
#!/bin/sh

vim -p `grep $* | cut -d: -f1 | sort -u | xargs`

I don't use grep much these days, in favour of the_silver_searcher, so I also have age:

Code:
#!/bin/sh

vim -p `ag $* | cut -d: -f1 | sort -u | xargs`

xvim creates a file, and immediately makes it executable; useful for little test scripts (saves a ^Z, chmod a+x, fg):

Code:
#!/bin/sh

touch $*
chmod a+x $*
vim -p $*
 
I've tested something like this before, but this is the first time it returned without consequence to the original window manager. One should have at least two xinitrc though, from a running xterm or simila.
Code:
xinit /usr/local/etc/X11/xinit/xinitrc-openbox -- /usr/local/bin/Xorg :1 -dpi 120 ttyve
then to return to the original window manager, just log out of Openbox. I have too little time to test with other window managers, though.
 
What's the reason for make fetchindex? Your portsnap fetch/update gets the index files already.

Please, read the following quote:
Make index will create the index by looking at your current ports tree. Make fetchindex will just fetch the index from a repository server. Fetchindex has the potential of fetching a different index compared to the ports tree that is actually on your system.

Posted in Thread 3566.
 
I saw that, actually, but didn't understand why you would want to your index to possibly be newer than the rest of your ports tree. From that same thread:

portsnap theoretically guarantees a consistent ports tree & INDEX: you're getting a snapshot of a sessile hierarchy.
 
I saw that, actually, but didn't understand why you would want to your index to possibly be newer than the rest of your ports tree. From that same thread:

Actually portsnap(8) is guaranteed to give you a ports tree and INDEX that are in sync. The reason for doing make fetchindex is when you're using SVN for fetching and updating the ports tree and you're too lazy to run make index. The slight discrepancy between the tree and the INDEX file usually doesn't cause any problems.
 
We're talking past each other. The script is not using svn. It is using portsnap fetch, and then:
Code:
/usr/sbin/portsnap update || exit 1
cd /usr/ports && make fetchindex || exit 1
I am asking why does it do this make fetchindex step? It is replacing the good index with one that may be out of sync with the rest of the ports tree.
 
I'll be glad, if this experimental notes will be useful for anyone.

device.map
Code:
(hd0) /usr/local/VirtualBox/vmm/vmd0
(cd0) /usr/home/openSUSE/openSUSE-13.2-DVD-x86_64.iso
vm0.sh
Code:
#!/bin/sh
#kldload vmm.ko
#kldload nmdm.ko
#truncate -s 8G /usr/local/VirtualBox/vmm/vmd0

# Installation
#grub-bhyve -m device.map -r cd0 -M 1024M suse0
#grub> linux (cd0)/boot/x86_64/loader/linux
#grub> initrd (cd0)/boot/x86_64/loader/initrd
#grub> boot

# Usage
grub-bhyve -d /boot/grub2 -m device.map -r hd0,msdos2 -M 1024M vm0
#grub> linux (hd0,msdos2)/boot/vmlinuz
#grub> initrd (hd0,msdos2)/boot/initrd
#grub> boot

bhyve \
-A -H -P -m 1024M \
-s 0:0,hostbridge \
-s 1:0,lpc \
-s 2:0,ahci-cd,/usr/home/openSUSE/openSUSE-13.2-DVD-x86_64.iso \
-s 3:0,ahci-hd,/usr/local/VirtualBox/vmm/vmd0 \
-s 4:0,virtio-net,tap0 \
-l com1,stdio \
vm0

#cu -l /dev/nmdm0B -s 9600

#bhyvectl --destroy --vm=suse0
#ifconfig tap1 destroy
#kldunload nmdm.ko
#kldunload vmm.ko
It is not "ready for use" script. It's fine in one sense - It works!
 
We're talking past each other. The script is not using svn. It is using portsnap fetch, and then:
Code:
/usr/sbin/portsnap update || exit 1
cd /usr/ports && make fetchindex || exit 1
I am asking why does it do this make fetchindex step? It is replacing the good index with one that may be out of sync with the rest of the ports tree.

make fetchindex brings a new INDEX-10 instead use the index with portsnap(8) (pre-generated one by another person).
Code:
% ls -al /usr/ports/IND*
-rw-r--r--  1 root  wheel  32320527 Oct 24 17:35 /usr/ports/INDEX-10
-rw-r--r--  1 root  wheel  32780684 Oct 24 17:35 /usr/ports/INDEX-9
 
Here's one to maintain ZFS snapshots with cron. It's inspired by ZfSnap, but does a few things differently and has a simpler interface. It's been working well for me, but it's new and hasn't been tested on OSes other than FreeBSD.

https://github.com/Jehops/zap
 
Hi, I'm searching for a 'clean script' after a clone of my FreeBSD Virtual Machine. The question is not really how to write the script but what to clean? SSH host keys, some /var/log/*, ...
Thanks you
 
Script to generate Openbox menu with selection of the default Sound output (uses /dev/sndstat and sysctl hw.snd.default_unit=X).

Looks like that:
2016-07-18-123832_3840x1200_scrot.png


Script itself:
Bash:
% cat ~/bin/__openbox_freebsd_sound.sh
#! /bin/sh

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
echo "<openbox_pipe_menu>"

echo "<item label=\"FreeBSD Sound Devices\">"
echo "  <action name=\"Execute\">"
echo "  <command>cat /dev/sndstat</command>"
echo "  </action>"
echo "</item>"

echo "<separator />"

if [ -e /dev/sndstat ]
then
  cat /dev/sndstat \
  | sed 1d \
  | grep play \
  | while read DEVICE
  do
  NUMBER=$( echo "${DEVICE}" | awk -F':' '{print $1}' | grep -o -E "[0-9]+" | sed 's|<|&lt;|g' | sed 's|>|&gt;|g' )
  CMD="sudo sysctl hw.snd.default_unit=${NUMBER}"
  NAME=$( echo "${DEVICE}" | sed 's|<|\&lt;|g' | sed 's|>|\&gt;|g' )
  echo "<item label=\"${NAME}\">"
  echo "  <action name=\"Execute\">"
  echo "  <command>${CMD}</command>"
  echo "  </action>"
  echo "</item>"
  done

  echo "<separator />"

  CURRENT=$( cat /dev/sndstat | grep 'default$' )
  NAME=$( echo "${CURRENT}" | sed 's|<|\&lt;|g' | sed 's|>|\&gt;|g' )
  echo "<item label=\"CURRENT: ${NAME}\" />"
else
  echo "<separator label=\"The /dev/sndstat file is not available.\" />"
fi

echo "</openbox_pipe_menu>"
 
Last edited by a moderator:
How to generate acceptance for ALL possible licenses in the FreeBSD Ports tree?

Code:
That way:
# grep -h -o -r '^LICENSE_NAME_.*=' /usr/ports \
  | sort -u \
  | tr -d '=' \
  | sed 's|LICENSE_NAME_||'g \
  | grep -v '${lic}' \
  | while read I
    do
      echo LICENSES_ACCEPTED+=${I}
    done
LICENSES_ACCEPTED+=3GPP
LICENSES_ACCEPTED+=ACUBE
LICENSES_ACCEPTED+=ADOBE
LICENSES_ACCEPTED+=APL1
LICENSES_ACCEPTED+=BSDCOMPATIBLE
LICENSES_ACCEPTED+=BSIML
LICENSES_ACCEPTED+=BuildLic
LICENSES_ACCEPTED+=CC0
LICENSES_ACCEPTED+=CCBYNCSAv3
LICENSES_ACCEPTED+=CCBYSAv3
LICENSES_ACCEPTED+=CCBYv3
LICENSES_ACCEPTED+=CCbyNC25
LICENSES_ACCEPTED+=CCbyNCSA2
LICENSES_ACCEPTED+=CCbyNCSA3
LICENSES_ACCEPTED+=CCbySA30
LICENSES_ACCEPTED+=CCbySA3
LICENSES_ACCEPTED+=CCbySA40
LICENSES_ACCEPTED+=CCbySA
LICENSES_ACCEPTED+=CLEARTHOUGHT
LICENSES_ACCEPTED+=COMMERCIAL
LICENSES_ACCEPTED+=CPL05
LICENSES_ACCEPTED+=CPL
LICENSES_ACCEPTED+=CUBE
LICENSES_ACCEPTED+=CW
LICENSES_ACCEPTED+=CeCILL
LICENSES_ACCEPTED+=CeCILL_C
LICENSES_ACCEPTED+=DMD
LICENSES_ACCEPTED+=DejaVu
LICENSES_ACCEPTED+=ECW
LICENSES_ACCEPTED+=EULA
LICENSES_ACCEPTED+=EUPL
LICENSES_ACCEPTED+=FAL
LICENSES_ACCEPTED+=FDK_AAC
LICENSES_ACCEPTED+=FFTPAK
LICENSES_ACCEPTED+=FLOSSE
LICENSES_ACCEPTED+=FPL
LICENSES_ACCEPTED+=GLEW
LICENSES_ACCEPTED+=GPLR
LICENSES_ACCEPTED+=HISIML
LICENSES_ACCEPTED+=HW
LICENSES_ACCEPTED+=IBM
LICENSES_ACCEPTED+=ICOT
LICENSES_ACCEPTED+=ICU
LICENSES_ACCEPTED+=IDPL
LICENSES_ACCEPTED+=IFL
LICENSES_ACCEPTED+=IPL
LICENSES_ACCEPTED+=ISO
LICENSES_ACCEPTED+=IUP
LICENSES_ACCEPTED+=JAIDL
LICENSES_ACCEPTED+=JDOM
LICENSES_ACCEPTED+=JYTHON
LICENSES_ACCEPTED+=LINUXLIBERTINE
LICENSES_ACCEPTED+=LinuxdocTools
LICENSES_ACCEPTED+=MSPAT
LICENSES_ACCEPTED+=NAIST
LICENSES_ACCEPTED+=NAISTL
LICENSES_ACCEPTED+=NEWRELIC
LICENSES_ACCEPTED+=NKF
LICENSES_ACCEPTED+=NODE
LICENSES_ACCEPTED+=OFL
LICENSES_ACCEPTED+=OPENLDAP
LICENSES_ACCEPTED+=OTHER
LICENSES_ACCEPTED+=PUBLIC_DOMAIN
LICENSES_ACCEPTED+=PerconaFT
LICENSES_ACCEPTED+=QPL10
LICENSES_ACCEPTED+=SENDMAIL
LICENSES_ACCEPTED+=SIL
LICENSES_ACCEPTED+=SpecialAuth
LICENSES_ACCEPTED+=TTYP0
LICENSES_ACCEPTED+=TWAIN
LICENSES_ACCEPTED+=TclTk
LICENSES_ACCEPTED+=UIUC
LICENSES_ACCEPTED+=UNLICENSE
LICENSES_ACCEPTED+=UNRAR
LICENSES_ACCEPTED+=VOLITION
LICENSES_ACCEPTED+=W3C
LICENSES_ACCEPTED+=WARSOW
LICENSES_ACCEPTED+=addl
LICENSES_ACCEPTED+=imlib2
LICENSES_ACCEPTED+=unknown

Then put all these LICENSES_ACCEPTED+=* lines into the /etc/make.conf file.
 
Here's how I see jailed userland versions:
Code:
% cat jls-version
#!/bin/sh
for JAIL in `jls | grep -v JID | awk '{print $3}'`; do 
    echo -n "$JAIL "
    jexec $JAIL freebsd-version
done | awk '{print $2 "  " $1}' | sort
'looks like this:
Code:
# jls-version
10.0-RELEASE-p12  dhcp
10.0-RELEASE-p12  jamclod
10.0-RELEASE-p12  ns
10.1-RELEASE-p31  ns-dmz
10.1-RELEASE-p37  dhcp-dmz
10.1-RELEASE-p37  funmax
10.1-RELEASE-p37  ns-inet
10.1-RELEASE-p37  www
10.2-RELEASE-p10  alf
10.2-RELEASE-p10  samba36a
10.2-RELEASE-p10  samba42a
10.2-RELEASE-p10  samba42b
10.2-RELEASE-p10  samba43a
10.2-RELEASE-p14  squid
10.2-RELEASE-p17  milk1-r
10.2-RELEASE-p20  mail
 
pdfetch, pre-fetching for poudriere

This is only useful on a painfully slow internet connection like mine at the moment (still waiting for DSL, using UMTS right now) -- parallel fetches often fail on me, so I now use this before running poudriere bulk:

Bash:
#!/bin/sh

PORTSLIST=/etc/pdports
PORTSDIR=/usr/local/poudriere/ports/default
PORT_DBDIR=/usr/local/etc/poudriere.d/11a-options
DISTDIR=/usr/local/poudriere/distfiles
MAKE_CONF=/usr/local/etc/poudriere.d/make.conf

for PORT in `cat ${PORTSLIST}`; do
    make __MAKE_CONF=${MAKE_CONF} \
        PORTSDIR=${PORTSDIR} PORT_DBDIR=${PORT_DBDIR} \
        DISTDIR=${DISTDIR} \
        -C ${PORTSDIR}/${PORT} fetch-recursive
done
just set the variables accordingly to your poudriere setup and run it.
 
Last edited by a moderator:
Here's what I do it's Custom :)

I don't bother with "non-living" backups. If I have a machine fail I want to slide over to the next one and continue (my college days - get the report *finished*). The cost of a (2nd hand) PC to house the backup disk in - minimal - the benefits - great.

I keep 2-3 machines synchronized BY HAND (rsync) and use RCS to keep EACH FILE incrementally saved. Here's how and why. It saves time and is more useful, and stable.

I use RCS whenever I edit a hand edited script or have hour of change to some edited file. This means there are two copies of each file: dir/file and dir/RCS/file,v. Loss of file? just co -l file - no need to (telnet/rsh/ssh) live running "backup". If somehow i delete a whole dir tree (very rare i do) ... i just rsync it from the other pc. no backup software needed. if i need another pc - i just copy the whole drive and boot the new pc - no restoring bacup needed. Another thing: say software installs and clobbers file: just co -l file. RCS is 100x more efficient than keeping incremental backups.

Caveate: only broadcast/synchronize file,RCS/file,v to all hosts (srdist file) by hand. Only a human realizes which copy is really the newer and has just edited changes - and unix time is too easily wrong to use time as an indicator! I tried a few schemes for automated: they have serious flaws of continually having A WRONG versioned file as newer - and that might make or break a whole backup as to if restoring it helps !! one file.

At first it sounds like work using RCS for each edit. But compared to using incremental backups it's a HUGE timesaver. And it also means you can shift between two PC at all times for reasons other than backups. Just to use 2 or at once.

I don't backup the OS because that can be re-installed - and likely changes anywho - whether i like it, or agree, or not. I hate incompatibility changes - often new releases are just damaged UNIX - just changed defaults and fewer features *available* due to hacking unix away from being unix. Not to mention: nothing works and everything must be hacked to fit joe schmo's hack to the core to fix his misfit past hacks :) These days - rebuilding/recompiling a complete distro fresh (assuming one has a script for doing so) takes not much longer than restoring a backup. Then one only needs their hand edited configs - which are RCS kept and kept outside system directories. I NEVER keep any hand-edited files in system directories, as these are systematically destroyed during re-install or upgrades or what.

I found out long ago that 1 backup isn't enough: there's a chance there's a fatal flaw with the back OR a chance you'll damage the backup (or the system being restored) when trying to restore. Therefore I prefer 3 living copies, just like accounting, always.

Caveate: beginners would not understand why to do this or what to do if they loose a file or a system. beginners would not understand how to keep hand-edited files clear of system directories that get clobbered. beginners would not necessarily appreciate or be able to keep 3 different kinds of PC up running living and synchronized - though the process simple when OS install is left aside (problem? os install is not really aside).

where to see scripts i use (here, only srdist really describes my convenient rsync wrapper 'srdist' simple file distributing):

https://sourceforge.net/projects/x-lfs-2010/
https://sourceforge.net/projects/ (see srdist)

i've been wanting to install bsd 4.3 or 10. btw i'm new to bsd on apple. but linux has allot in common (ie, early linux had mostly/wholey ripped bsd4.3 userland)
 
as far as having a backup disk and restoring a whole computer from an old backup?

i've never done it even once with unix. why is because it's never really my intent to do that.

i've only ever installed OS fresh and restored my personal files from still (incremental) backups. i never want an os that failed and caused a re-install or even it's near image ! i frequently want something different with OS if i'm even bothering. and for me: it's very rare to do.

restoring personal files from an whole disk backup is a process i decided was too timely to keep doing - all that damn "cd dir; ls -al" is timely. rcs and rsync take a little extra time when "saving changes to files", but i would argue pay off.
 
search for the port and installs, if it is already installed it reinstalls

Bash:
#!/bin/sh

NETBSD_PORTS="/usr/pkgsrc"
FREEBSD_PORTS="/usr/ports"

if [ `uname -s` == "NetBSD" ];
  then
  if [ -e "/usr/pkg/bin/$1" ];
    then
    find $NETBSD_PORTS -name $1 -exec make reinstall clean -C {} \;
  else
    find $NETBSD_PORTS -name $1 -exec make install clean -C {} \;
  fi

elif [ `uname -s` == "FreeBSD" ];
  then
  if [ -e "/usr/local/bin/$1" ];
    then
    find $FREEBSD_PORTS -name $1 -exec make reinstall clean -C {} \;
  else
    find $FREEBSD_PORTS -name $1 -exec make install clean -C {} \;
  fi
fi
 
Last edited by a moderator:
There are a few things wrong with it. It'll work for basic things like sysutils/tmux when the executable happens to have the same name as the port. But a lot of ports use names that don't reflect the executables.

Probably a better solution would be something like this:
Code:
if [ -e "/usr/local/bin/$1" ]; then
    PORTNAME=`pkg which -oq /usr/local/bin/$1`
    make reinstall clean -C "${FREEBSD_PORTS}/${PORTNAME}" 
fi

Code:
# pkg which -oq /usr/local/bin/mysql
databases/mysql57-client
See pkg-which(8).

There's probably a cool pkg-rquery(8) to find packages for executables that aren't installed. But care must be taken, there are several different ports providing /usr/local/bin/mysql for example (think MySQL 5.5, 5.6, 5.7, MariaDB etc.). And all of them conflict with any of the others.
 
There are a few things wrong with it. It'll work for basic things like sysutils/tmux when the executable happens to have the same name as the port. But a lot of ports use names that don't reflect the executables.

Probably a better solution would be something like this:
Code:
if [ -e "/usr/local/bin/$1" ]; then
    PORTNAME=`pkg which -oq /usr/local/bin/$1`
    make reinstall clean -C "${FREEBSD_PORTS}/${PORTNAME}"
fi

Code:
# pkg which -oq /usr/local/bin/mysql
databases/mysql57-client
See pkg-which(8).

There's probably a cool pkg-rquery(8) to find packages for executables that aren't installed. But care must be taken, there are several different ports providing /usr/local/bin/mysql for example (think MySQL 5.5, 5.6, 5.7, MariaDB etc.). And all of them conflict with any of the others.

thank you, I will implement the script
 
Back
Top