2b044 Useful scripts - Page 8 - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, C++, Python, Perl, Shell, etc.

Reply
 
Thread Tools Display Modes
  #176  
Old November 21st, 2012, 23:44
kpedersen kpedersen is offline
Member
 
Join Date: Apr 2009
Posts: 682
Thanks: 9
Thanked 104 Times in 75 Posts
Default

The following script disables the fan if the temp is lower than 45 and then enables it again at 55.

I know this isn't a brilliant idea but for some reason my Thinkpad x61 keeps its fan running trying to reach a very low temperature. It becomes quite cold for my wrist and quite uncomfortable for my lap

This will only work when using the acpi_ibm module.

Code:
#!/bin/sh

disable_fan()
{
  echo "disable"
  sysctl dev.acpi_ibm.0.fan=0
}

enable_fan()
{
  echo "enable"
  sysctl dev.acpi_ibm.0.fan=1
}

try_disable_fan()
{
  if [ `sysctl -n dev.acpi_ibm.0.fan` = 1 ]; then
    disable_fan
  fi
}

try_enable_fan()
{
  if [ `sysctl -n dev.acpi_ibm.0.fan` = 0 ]; then
    enable_fan
  fi
}

if [ `id -u` != 0 ]; then
  echo "Error: Must be root"
  exit 1
fi

while [ true ]; do

  HIGHEST_TEMP=0
  TEMPERATURES=`sysctl -n dev.acpi_ibm.0.thermal`

  for TEMP in $TEMPERATURES; do
    if [ $TEMP -gt $HIGHEST_TEMP ]; then
      HIGHEST_TEMP=$TEMP
    fi
  done

  if [ $HIGHEST_TEMP -gt 55 ]; then
    try_enable_fan
  elif [ $HIGHEST_TEMP -lt 45 ]; then
    try_disable_fan
  fi

  sleep 5

done
Reply With Quote
  #177  
Old December 3rd, 2012, 11:05
nickednamed nickednamed is offline
Junior Member
 
Join Date: Jan 2011
Posts: 85
Thanks: 32
Thanked 6 Times in 2 Posts
Default

This is the first script I've ever written. It's not that pretty, it isn't overly user friendly, and I borrowed [stole] a lot of it from taz - http://forums.freebsd.org/showpost.p...45&postcount=5

It is based on taz's script but allows me to quickly set up different VirtualBox VMs without having to edit the script:

Code:
#!/bin/sh

#-----------------------------------------------------------------------
#CONFIG
#-----------------------------------------------------------------------

echo "Type name of new VM:" 
read vmName

echo "Type full path for new VM:"
read hdPath

echo "Type VM disk size in megabytes:"
read hdSize

echo "Type amount of RAM to use in megabytes:"
read ramSize

echo "Type amount of RAM for GPU to use in megabytes:"
read gpuRamSize

echo "Type VM OS Type:"
read osType

echo "Type full path to guest installtion media or ISO:"
read guestISO

echo "Type host network interface name:"
read nic

#create folder for virtual hard disk image
if [ ! -d $hdPath ]
then 
    mkdir $hdPath
fi

#-----------------------------------------------------------------------
#CREATE
#-----------------------------------------------------------------------

#create a new virtual hard disk image.
VBoxManage createhd --filename $hdPath/$vmName.vdi --size $hdSize

#create a new XML virtual machine definition file
VBoxManage createvm --name $vmName --ostype $osType --register

#add an IDE controller with a DVD drive attached, and the install ISO inserted into the drive. Set "--medium none" to detach all.
VBoxManage storagectl $vmName --name "IDE Controller" --add ide
VBoxManage storageattach $vmName --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium $hdPath/$vmName.vdi
VBoxManage storageattach $vmName --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium $guestISO

#set boot order
VBoxManage modifyvm $vmName --boot1 dvd --boot2 disk --boot3 none --boot4 none

#set I/O APIC support
VBoxManage modifyvm $vmName --ioapic on

#set the amount of RAM
VBoxManage modifyvm $vmName --memory $ramSize

#set the amount of RAM for virtual graphics card
VBoxManage modifyvm $vmName --vram $gpuRamSize

#set network mode(briged,NAT...)
VBoxManage modifyvm $vmName --nic1 bridged --bridgeadapter1 $nic

#enable USB support
VBoxManage modifyvm $vmName --usb on

#enable sound
VBoxManage modifyvm $vmName --audio oss --audiocontroller ac97
I decided not to make it too fancy, requiring user to write full paths because the default machine directory may vary from machine to machine.

Last edited by nickednamed; February 26th, 2013 at 23:50.
Reply With Quote
  #178  
Old December 7th, 2012, 09:54
lockdoc lockdoc is offline
Member
 
Join Date: Jul 2009
Posts: 122
Thanks: 3
Thanked 6 Times in 5 Posts
Default

Hi,
here is another one that will let you create jails on a non-zfs filesystem (without creating it via snapshots) easily, including a couple of default config files (rc.conf, fstab, etc) and does also perform a couple of pre-checks (dir not empty, dir available, etc):
https://github.com/lockdoc/freebsd-t...create-jail.sh
__________________
https://github.com/lockdoc
Reply With Quote
  #179  
Old December 7th, 2012, 16:45
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 832
Thanks: 77
Thanked 120 Times in 80 Posts
Default

Here are some really really simple ones I used before as a basis for some of my other more specific scripts;

This one just check online status using tcp connect; returns online/offline.
Code:
#!/bin/sh
result="offline"
for i in 1 2 3; do      
        /usr/local/bin/netcat -w 5 -z www.google.com 80 2>/dev/null
        if [ $? = "0" ]; then
    result="online"
                break
        fi
done
echo $result
This one uses above script; It checks every 5 min for external IP and compares with last stored IP in a file. If it changes, send me an email and store new IP as well as date of change. Over time you get a bunch of different IPs you ever used (if you aren't on static that is).

Code:
#!/bin/sh
test=`$HOME/bin/test-if-online`
if [ $test = "online" ]; then
        current_IP=`tail -1 /var/tmp/current_ip_address.tmp | awk '{print $3}'`
        new_IP=`/usr/local/bin/curl -s ifconfig.me/ip`
        if [ $current_IP != $new_IP ]; then
                current_index=`tail -1 /var/tmp/current_ip_address.tmp | awk '{print $1}'`
                new_index=`expr $current_index + 1`
                new_date=`date "+%Y-%m-%d_%H.%M"`
                echo "$new_index $new_date $new_IP" >> /var/tmp/current_ip_address.tmp
                # -- Send mail
                tail -1 /var/tmp/current_ip_address.tmp | /usr/local/sbin/ssmtp me@gmail.com
        fi
fi
This one for ports, doesn't need explaining
Code:
#!/bin/sh
#update
/usr/local/bin/svn update /usr/ports
#any new?
pkg version -o -l \< | sed 's/[ <]//g' | tee $HOME/newports.001
#any vulns?
pkg audit -F | tee /var/tmp/portaudit_report.tmp
#any new updating?
pkg updating -d $(date -j -f "%s" "$(pkg query -a %t | sort | tail -1)" "+%Y%m%d")
One liner that shows total amount used by zfs snapshots on whole pool (not sure if there's an actual command for this)

Code:
#!/bin/sh
echo "$(zfs get -t filesystem -r -H -p usedbysnapshots zpool | awk '{print $3}' | awk '{total = total + $1}END{print total}') \
        / 1024 / 1024 / 1024" | bc
Reply With Quote
The Following User Says Thank You to bbzz For This Useful Post:
segfault (December 7th, 2012)
  #180  
Old December 9th, 2012, 19:18
naveenbeast naveenbeast is offline
Junior Member
 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Reinstalling FreeBSD and the ports and the updates can become a real pain in the ass. I wrote this script up to assist me with exactly that. This script makes clang the default compiler, applies optimizations to make.conf, installs updates via freebsd-update and updates the port tree with portsnap, installs xorg, i3 window manager, installs transmission, install firefox (or chromium), installs the linux base files, installs and sets up flash player 11, configure xorg, and configure rc.conf to enable hald,dbus and the linux compatibility layer. It also installs utilities like subversion, wget, nano, bash, portmaster and portupgrade. Tested on FreeBSD 9.1 RC3 install.

fbsd_setup.sh
Code:
#!/bin/sh
#By Naveen Mathew of the year 1998! :p

echo 'Configuring make.conf ....'
#Lets get rid of God forsaken gcc...
echo 'Changing default compiler from gcc to clang llvm ....'
echo '#Use the clang llvm compiler instead of gcc' >> /etc/make.conf
echo 'CC=clang' >> /etc/make.conf
echo 'CXX=clang++' >> /etc/make.conf
echo 'CPP=clang-cpp' >> /etc/make.conf

#Here we start optimizing
echo 'Adding optimization flags ....'
echo ' ' >> /etc/make.conf
echo '#Optimizations' >> /etc/make.conf
echo 'CFLAGS=-O2 -pipe -fno-strict-aliasing' >> /etc/make.conf
echo 'COPTFLAGS=-O2 -pipe -funroll-loops -ffast-math -fno-strict-aliasing' >> /etc/make.conf
echo 'OPTIMIZED_CFLAGS=YES' >> /etc/make.conf
echo 'BUILD_OPTIMIZED=YES' >> /etc/make.conf
echo 'WITH_OPTIMIZED_CFLAGS=YES' >> /etc/make.conf
echo '#Compiler options and optimizations added by the Jarvian OS configuration script' >> 
/etc/make.conf

#Update the system and ports tree
echo 'Updating system via freebsd-update ....'
freebsd-update fetch install
echo 'Updating ports tree ....'
cd /usr/ports && portsnap fetch update

#Installation of applications
echo 'Installing basic GUI applications ....'
cd /usr/ports/x11/xorg && make BATCH=Yes install clean
cd /usr/ports/x11-wm/i3 && make BATCH=Yes install clean
cd /usr/ports/x11/dmenu && make BATCH=Yes install clean
cd /usr/ports/x11/i3status && make BATCH=Yes install clean

echo 'Installing utilities ....'
cd /usr/ports/devel/subversion && make BATCH=Yes install clean
cd /usr/ports/ftp/wget && make BATCH=Yes install clean
cd /usr/ports/editors/nano && make BATCH=Yes install clean
cd /usr/ports/shells/bash && make BATCH=Yes install clean
cd /usr/ports/ports-mgmt/portmaster && make BATCH=Yes install clean
cd /usr/ports/ports-mgmt/portupgrade && make BATCH=Yes install clean

echo 'Installing everyday applications ....'
cd /usr/ports/net-p2p/transmission25 && make BATCH=Yes install clean #Transmission25 because the 
other one is broken...
cd /usr/ports/multimedia/vlc && make BATCH=Yes install clean
#Choose either one. Or install both :p
#If you prefer Chromium, comment the Firefox line and uncomment the Chromium line
#cd /usr/ports/www/chromium && make BATCH=Yes install clean #Chromium line
cd /usr/ports/www/firefox && make BATCH=Yes install clean #Firefox line

#Install linux base files and flash
echo 'Installing and setting up Linux base files and flash ....'
cd /usr/ports/emulators/linux_base_f10 && make BATCH=Yes install clean
cd /usr/ports/www/nspluginwrapper && make BATCH=Yes install clean
cd /usr/ports/www/linux-f10-flashplugin11 && make BATCH=Yes install clean
mkdir /usr/local/lib/browser_plugins
cd /usr/local/lib/browser_plugins && ln -s 
/usr/local/lib/npapi/linux-f10-flashplugin/libflashplayer.so
echo ''
echo 'Activating linux module with kldload ....'
kldload linux
echo 'Installing flash player plugin for user ....'
nspluginwrapper -v -a -i
echo ''

#Configure installed programs
echo "Configuring Xorg ...."
echo ' ' >> /etc/rc.conf && echo '#Added by Jarvian OS configuration script" >> /etc/rc.conf
echo 'hald_enable="YES"' >> /etc/rc.conf
echo 'dbus_enable="YES"' >> /etc/rc.conf
echo 'linux_enable="YES"' >> /etc/rc.conf
Xorg -configure
cp ~/xorg.conf.new /etc/X11/xorg.conf

#Below is code for the future. You can just uncomment it and use it now though. It works.
#echo 'Configuring i3 window manager. Windows (Super) key is the default modifier'
#echo 'The config file is located in ~/.i3/config'
#mkdir ~/.i3
#cp jconfig/config ~/.i3/config

Last edited by DutchDaemon; December 9th, 2012 at 19:45. Reason: Proper formatting: http://forums.freebsd.org/showthread.php?t=8816
Reply With Quote
  #181  
Old December 10th, 2012, 05:04
naveenbeast naveenbeast is offline
Junior Member
 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chromium in root mode

Here is another script I use. Its for chromium to launch in root mode. Before launching the script, create a user with username chrome and everything else as default. Put whatever you want for full name. Then place this script in /usr/local/bin and just execute 'chrome-root' in a terminal.

chrome-root
Code:
#!/bin/sh
#Chrome with data dir execution 
#For root accounts

echo 'Launching Google Chrome for root ....'
chrome --user-data-dir=/home/chrome
Put the code into a file called chrome-root and chmod 777 it. Then place it in /usr/local/bin and enjoy life. :p
Reply With Quote
  #182  
Old December 18th, 2012, 16:07
naveenbeast naveenbeast is offline
Junior Member
 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Second revision of reinstall script.

The second version of my initial reinstall script.
It now installs and configures nearly everything for a day to day FreeBSD use.
It also now provides to methods of installing packages. Pkg_add or Ports.
It also does a bunch of other stuff.
To install, just extract the attachment below and run the setup0.setup file.
It will do everything else.
P.S pkg_add is too outdated for my preference so I recommend ports by all chances.
Blackfire.tar.gz
Reply With Quote
  #183  
Old December 18th, 2012, 18:30
graudeejs's Avatar
graudeejs graudeejs is offline
Style(9) Addict
 
Join Date: Nov 2008
Location: Riga, Latvia
Posts: 4,530
Thanks: 424
Thanked 612 Times in 479 Posts
Default

Quote:
Originally Posted by naveenbeast View Post
The second version of my initial reinstall script.
It now installs and configures nearly everything for a day to day FreeBSD use.
It also now provides to methods of installing packages. Pkg_add or Ports.
It also does a bunch of other stuff.
To install, just extract the attachment below and run the setup0.setup file.
It will do everything else.
P.S pkg_add is too outdated for my preference so I recommend ports by all chances.
Attachment 1730
The fact that it copies (actually it would fail) hidden directory makes me suspicious about this scripts.
And while I looked briefly at source and it looks ok, I would never ever use it (because of style how it's "installed", not to mention it will modify every aspect of system).
If anyone want's to try out, I recommend you check entire source (looks safe, but you never know)


@naveenbeast, check how I solve app configuration problem:
https://github.com/graudeejs/dot.files
https://github.com/graudeejs/dot.vim
https://github.com/graudeejs/dot.fvwm
https://github.com/graudeejs/desktop (needs updating)

System config is different matter, and I won't discuss it here (don't have that much time).

P.S.
At first I thought your setup0.setup will prepare environment to install some root-kit. No joking.
Reply With Quote
  #184  
Old December 18th, 2012, 22:11
Zare Zare is offline
Member
 
Join Date: Nov 2008
Location: Split, Dalmatia
Posts: 360
Thanks: 26
Thanked 50 Times in 41 Posts
Default

Quote:
Originally Posted by naveenbeast View Post
Reinstalling FreeBSD and the ports and the updates can become a real pain in the ass. I wrote this script up to assist me with exactly that. This script makes clang the default compiler, applies optimizations to make.conf, installs updates via freebsd-update and updates the port tree with portsnap, installs xorg, i3 window manager, installs transmission, install firefox (or chromium), installs the linux base files, installs and sets up flash player 11, configure xorg, and configure rc.conf to enable hald,dbus and the linux compatibility layer. It also installs utilities like subversion, wget, nano, bash, portmaster and portupgrade. Tested on FreeBSD 9.1 RC3 install.

fbsd_setup.sh
Code:
...
-fno-stict-aliasing isn't an optimization. It will disable aliasing by type for C/C++ compiler. Aliasing by type actually optimizes, because it assumes that different type pointers won't point to same address. If you need this option for your programs, address the issue in the code. There are a lot of different examples for C, like simply using those pointers in union.

IMHO, it's only safe to put these gcc flags in /etc/make.conf : -pipe (forces programs in compilation process to communicate via pipes rather than temporary files), -O[n] (optimization level). Be sure to check out what flags are automatically enabled by various optimization levels. Even -march should be set by CPUTYPE variable, not CFLAGS.

Actually, it's best to leave out CFLAGS completely for standard FreeBSD usage.
Reply With Quote
  #185  
Old December 18th, 2012, 22:23
kpa kpa is offline
Giant Locked
 
Join Date: Jul 2010
Location: People's Technocratic Republic of Finland
Posts: 2,139
Thanks: 46
Thanked 510 Times in 433 Posts
Default

You're asking for a lot of trouble by running www/chromium as root, please don't do that. Whatever problems you have with it that could be solved by running it as root, find a another safer solution.
Reply With Quote
  #186  
Old January 3rd, 2013, 08:04
jrm's Avatar
jrm jrm is online now
Member
 
Join Date: Nov 2008
Location: Tralfamadore
Posts: 543
Thanks: 64
Thanked 131 Times in 98 Posts
Default mode-line script

Here is a script that gives me the x11-wm/stumpwm mode-line below.



I'm very new to shell scripting, so there is likely a better way to do much of what I did. Criticism is welcomed. I hard coded a few things that are specific to my system (e.g. I know the network interfaces are either wlan0 or em0), but it shouldn't too difficult to adapt to different setups. I also use net/ifstat to get the network traffic data, because I couldn't get it to work with netstat.

ml
Code:
see next post

Last edited by jrm; January 6th, 2013 at 03:27.
Reply With Quote
  #187  
Old January 4th, 2013, 06:41
jrm's Avatar
jrm jrm is online now
Member
 
Join Date: Nov 2008
Location: Tralfamadore
Posts: 543
Thanks: 64
Thanked 131 Times in 98 Posts
Default

Take two. The io and istat scripts are no longer needed and the script processes die when you exit x11-wm/stumpwm.

ml.sh
Code:
#!/usr/bin/env sh

stump_pid=`pgrep -a -n stumpwm`

mem_tot=`awk '/real memory/ {print $4;exit}' /var/run/dmesg.boot`
mem_tot=$(( ${mem_tot}/1024/1024 ))

# while stumpwm is still running
while kill -0 $stump_pid > /dev/null 2>&1; do
	hn=`hostname -s`
	date=`date '+%a %b %e %H:%M:%S'`
	disk=`df -t ufs -h | awk 'END{print $3" "$2" "$4}'`
	set -- $disk; ds_usd=$1; ds_tot=$2; ds_avl=$3;
	ds_usd_val=${ds_usd%?}; ds_usd_unt=${ds_usd#$ds_usd_val}
	ds_tot_val=${ds_tot%?}; ds_tot_unt=${ds_tot#$ds_tot_val}
	ds_avl_val=${ds_avl%?}; ds_avl_unt=${ds_avl#$ds_avl_val}
	io=`iostat -x -w2 -c2 ada0 | awk 'END{print $4" "$5}'`
	set -- $io; dr=$1; dw=$2
	net=
	if ifconfig wlan0 2> /dev/null | grep -q inet; then
		ifn="wlan0"
		net=`/usr/local/bin/ifstat -i wlan0 1 1 | awk 'END{print $1" "$2}'`
	elif ifconfig em0 | grep -q inet; then
		ifn="em0"
		net=`/usr/local/bin/ifstat -i em0 1 1 | awk 'END{print $1" "$2}'`
	fi
	[ -z "$net" ] || { set -- ${net}; if_dl=$1; if_ul=$2; }
	sysctl=`sysctl -n dev.cpu.0.freq hw.acpi.thermal.tz0.temperature\
    hw.acpi.battery.life hw.acpi.battery.state hw.pagesize\
    vm.stats.vm.v_inactive_count vm.stats.vm.v_free_count\
    vm.stats.vm.v_cache_count`;
	set -- $sysctl; cpu_freq=$1; cpu_tmp=${2%?}; bat_lf=$3; bat_st=$4;
	page_siz=$5; inct_cnt=$6; free_cnt=$7; cach_cnt=$8
	mem_use=$(( ${mem_tot}-(${inct_cnt}+${free_cnt}+${cach_cnt})*${page_siz}/1024/1024 ))
	vol=`mixer -s vol | sed 's/vol [0-9]*://'`
	ML="^[^B^7*$hn^] ^[^8*C^] %4d^[^9*MHz^] ${cpu_tmp}^[^9*C^] ^[^8*M^]\
 %4d^[^9*M^]/${mem_tot}^[^9*M^] ^[^8*D^] ${ds_usd_val}^[^9*${ds_usd_unt}^]/\
${ds_tot_val}^[^9*${ds_tot_unt}^] ${ds_avl_val}^[^9*${ds_avl_unt}^] %8.1f^[^9*K/s^] %8.1f^[^9*K/s^]"
	[ -z "${net}" ] || ML="${ML} ^[^8*$ifn^] %6.1f^[^9*K/s^] %6.1f^[^9*K/s^]"
	ML="${ML} ^[^8*B^] ${bat_lf}"
	if [ ${bat_st} = '0' ] || [ ${bat_st} = '2' ]; then
		ML="${ML}^[^9*-^]"
	else
 		ML="${ML}^[^9*+^]"
	fi
	ML="${ML} ^[^8*V^] ${vol} | ^[^B^7*${date}^]"
	if [ -z "${net}" ]; then
		printf "$ML" ${cpu_freq} ${mem_use} ${dr} ${dw} > /tmp/ml-info.txt
	else
		printf "$ML" ${cpu_freq} ${mem_use} ${dr} ${dw} ${if_ul} ${if_dl} > /tmp/ml-info.txt
	fi
done

Last edited by jrm; January 6th, 2013 at 03:26.
Reply With Quote
  #188  
Old January 5th, 2013, 17:16
jrm's Avatar
jrm jrm is online now
Member
 
Join Date: Nov 2008
Location: Tralfamadore
Posts: 543
Thanks: 64
Thanked 131 Times in 98 Posts
Default script to start/stop wireless connections

Code:
#!/usr/bin/env sh

# set this to your wireless interface
wl_if=iwn0

# Usage: ws [start <ssid> | stop | list]

if [ -z $wl_if ]; then
	printf "wl_if is not set to the wireless interface.\n"
	exit
fi

extra() { printf "Extra arguments are being ignored.\n"; }

stop_ws()
{ 
	ifconfig wlan0 destroy
	printf "wlan0 destroyed\n"
}

usage() { printf "Usage: ws [start <ssid> | stop | list]\n"; exit; }

[ $# -eq 0 ] && usage

if [ $1 = 'start' ]; then
	[ $# -lt 2 ] && usage
	[ $# -gt 2 ] && extra
	ifconfig wlan0 > /dev/null 2>&1 && stop_ws
	ifconfig wlan0 create wlandev ${wl_if}
	ifconfig wlan0 ssid $2
	if !(grep -q "^[[:space:]]*ssid=['\"'']\?$2['\"'']"\
 /etc/wpa_supplicant.conf); then
		printf "Adding an entry for $2 in /etc/wpa_supplicant...\n"
		stty -echo
		read -p "Passphrase: " ps_phr
		stty echo
		printf "\n** If password is incorrect, you have to fix\
 /etc/wpa_supplicant.conf before running "
		printf `basename $0`; printf " again. **\n"
		printf "\nnetwork={\n\tssid=\"$2\"\n\tpsk=\"${ps_phr}\"\n}\n" >>\
 /etc/wpa_supplicant.conf
	fi
	wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
	printf "Associating with $2"
	while !(ifconfig wlan0 | grep -q associated); do
		printf "."
		sleep 1
	done
	printf "\nAssociated to $2\nFlushing route...\nCalling dhclient...\n"
	dhclient wlan0
	route change default -ifp wlan0
elif [ $1 = 'stop' ]; then
	[ $# -gt 1 ] && extra
	if ifconfig wlan0 > /dev/null 2>&1; then stop_ws
	else printf "The interface wlan0 does not exist.\n"
	fi
elif [ $1 = 'list' ]; then
	[ $# -gt 1 ] && extra
	ifconfig wlan0 > /dev/null 2>&1 || ifconfig wlan0 create wlandev ${wl_if}
	ifconfig wlan0 | grep -q 'status: associated' || ifconfig wlan0 up
	ifconfig wlan0 list scan
else
	usage
fi

Last edited by jrm; March 3rd, 2013 at 05:53.
Reply With Quote
  #189  
Old February 9th, 2013, 15:36
Michael-Sanders Michael-Sanders is offline
Junior Member
 
Join Date: Feb 2012
Posts: 38
Thanks: 13
Thanked 7 Times in 6 Posts
Default

Just discovered this, very handy (please excuse if its a repeat):

Code:
# obtain console screen width/height

w=$(tput cols)   # width
h=$(tput lines)  # height
x=$(expr $w / 2) # 1/2 width
y=$(expr $h / 8) # 1/8 height, etc...
Reply With Quote
  #190  
Old February 9th, 2013, 16:43
wblock@'s Avatar
wblock@ wblock@ is offline
Moderator
 
Join Date: Sep 2009
Location: Milky Way galaxy
Posts: 7,851
Thanks: 445
Thanked 1,829 Times in 1,495 Posts
Default

"Arithmetic expansion" can be used to avoid needing expr(1):
Code:
w=$(tput cols)   # width
h=$(tput lines)  # height
x=$((w / 2))
y=$((h / 8))
Reply With Quote
The Following User Says Thank You to wblock@ For This Useful Post:
Michael-Sanders (February 9th, 2013)
  #191  
Old February 9th, 2013, 18:07
Michael-Sanders Michael-Sanders is offline
Junior Member
 
Join Date: Feb 2012
Posts: 38
Thanks: 13
Thanked 7 Times in 6 Posts
Default

ahh!
Reply With Quote
  #192  
Old February 15th, 2013, 23:17
bkouhi bkouhi is offline
Member
 
Join Date: Sep 2011
Posts: 167
Thanks: 192
Thanked 77 Times in 53 Posts
Default

This simple script allow you to surf in ports tree and discover wonderful applications by reading pkg-descr:


Code:
#!/bin/sh
trap 'exit 0' 2

for i in $1/*
do
  if [ -d $i ] ; then
       cd $i
       clear
       echo -e "Name: $i\n"
        if [ -f pkg-descr ]; then
               less pkg-descr
        else
               echo -e "pkg-descr not found!\n"
        fi
        cd ..
  fi
done
Usage:
% ./script /usr/ports/category

To read next pkg-descr just press q, to quit the program press CTRL+C.
HTH
Reply With Quote
  #193  
Old February 16th, 2013, 07:53
Michael-Sanders Michael-Sanders is offline
Junior Member
 
Join Date: Feb 2012
Posts: 38
Thanks: 13
Thanked 7 Times in 6 Posts
Default

Very nifty script. =)

(Just thinking aloud here) would be handy if the end user could install a given package/port using your idea. Something like:

Code:
<pseudo-code>

dialog --yesno --title "Install $PKG_NAME?" "$PKG_DESCR" $HEIGHT $WIDTH
[ $? -eq 0 ] && func_add_pkg $PKG_NAME || func_next_routine

</pseudo-code>
Reply With Quote
  #194  
Old February 16th, 2013, 13:12
jb_fvwm2 jb_fvwm2 is offline
Senior Member
 
Join Date: Nov 2008
Posts: 1,399
Thanks: 61
Thanked 147 Times in 131 Posts
Default

I once cat-ed together all the pkg-descr in a category or two so I could read them all at once; though had to script something up in a wrapper to put the port pwd below or above each snippet (file), so one could ascertain the port one was reading of... that was many years ago though.
__________________
Using /lookat/ with zsh/grep/find/aliases/pipes/portmaster and /var/db/pkg/ flat files to meteorically speed port installs/upgrades forever hopefully...
Reply With Quote
  #195  
Old February 18th, 2013, 20:44
Beastie Beastie is offline
Senior Member
 
Join Date: Mar 2009
Location: /dev/earth0
Posts: 1,715
Thanks: 1
Thanked 310 Times in 250 Posts
Default

Instead of decoding URL hex codes manually or relying on JavaScript-based online decoders, you can use the following one liner:
Code:
#!/bin/sh

echo "$1" | sed 's/%24/$/g;s/%26/\&/g;s/%2B/+/g;s/%2C/,/g;s/%2F/\//g;s/%3A/:/g;s/%3B/\;/g;s/%3D/=/g;s/%3F/?/g;s/%40/@/g;'
Example:
Code:
urldec.sh 'http%3A%2F%2Fforums.freebsd.org%2Fshowthread.php%3Ft%3D737'
(sorry for the second code tag, but the URL is not displayed properly in a cmd tag)
__________________
May the source be with you!
Reply With Quote
The Following User Says Thank You to Beastie For This Useful Post:
redw0lfx (February 19th, 2013)
  #196  
Old March 8th, 2013, 20:11
graudeejs's Avatar
graudeejs graudeejs is offline
Style(9) Addict
 
Join Date: Nov 2008
Location: Riga, Latvia
Posts: 4,530
Thanks: 424
Thanked 612 Times in 479 Posts
Default

Remove all gems:
Code:
$ gem list | awk '{print $1}' | xargs -I '%' -n 1 gem uninstall '%' -q -a -I
Reply With Quote
  #197  
Old March 8th, 2013, 22:55
jb_fvwm2 jb_fvwm2 is offline
Senior Member
 
Join Date: Nov 2008
Posts: 1,399
Thanks: 61
Thanked 147 Times in 131 Posts
Default

Apologies if this is a repost. I have several directories each containing .tbz which unfortunately accumulate several versions of each without an easy method of parsing out the extra ones. This week discovering that
Code:
bsddiff /dir1 /dir2
enables easy copy to-fro so that both are in sync, the only remaining task is to delete the older versions.
Two ports necc. for the following...
Code:
/usr/local/bin/gnuls -oSr | sort -k 4 | sort -k 8 | /usr/local/bin/lookat
[two xterm/screen/tmux/or the first .tbz directory ]
[ then the delete is already in history for the next .tbz directory ]
...after the following is complete [If I've explained it adequately]

That is in one xterm. In the first .tbz directory, parsing down thru the lookat in one terminal, one can construct a long delete-older /bin/rm -v line in a different xterm/tmux/screen [...and recall it from history to delete from any/all the additional tbz
directories.] [Very handy here, where a thumbdrive also is a repository for them.] I
had thought of constructing a perl equivalent, but the certainty of knowing that the
task is done without error at the command line is worth the additional time to
delete manually, at least for now... [Someday... pkg_info for example wants to unextract the .tbz to find its origin, making the process somewhat
too costly in CPU time...]

This is also useful if one wishes to purge older distfiles from /usr/ports/distfiles, (just one instance though) though usually one may not need to do that often [and it should then be repeated in /usr/ports/distfiles/gnome2 etc.]
__________________
Using /lookat/ with zsh/grep/find/aliases/pipes/portmaster and /var/db/pkg/ flat files to meteorically speed port installs/upgrades forever hopefully...

Last edited by jb_fvwm2; March 9th, 2013 at 18:18. Reason: clarification and additional instructions
Reply With Quote
The Following User Says Thank You to jb_fvwm2 For This Useful Post:
bkouhi (March 8th, 2013)
  #198  
Old March 9th, 2013, 10:51
graudeejs's Avatar
graudeejs graudeejs is offline
Style(9) Addict
 
Join Date: Nov 2008
Location: Riga, Latvia
Posts: 4,530
Thanks: 424
Thanked 612 Times in 479 Posts
Default

Quote:
Originally Posted by Beastie View Post
Instead of decoding URL hex codes manually or relying on JavaScript-based online decoders, you can use the following one liner:
Code:
#!/bin/sh

echo "$1" | sed 's/%24/$/g;s/%26/\&/g;s/%2B/+/g;s/%2C/,/g;s/%2F/\//g;s/%3A/:/g;s/%3B/\;/g;s/%3D/=/g;s/%3F/?/g;s/%40/@/g;'
Example:
Code:
urldec.sh 'http%3A%2F%2Fforums.freebsd.org%2Fshowthread.php%3Ft%3D737'
(sorry for the second code tag, but the URL is not displayed properly in a cmd tag)
Here's a much better ruby script
Code:
#!/usr/bin/env ruby

require 'cgi'

ARGV.each do |encoded_url|
  puts CGI::unescape(encoded_url)
end
Reply With Quote
  #199  
Old March 9th, 2013, 16:10
vermaden's Avatar
vermaden vermaden is offline
Giant Locked
 
Join Date: Nov 2008
Location: pl_PL.lodz
Posts: 2,210
Thanks: 60
Thanked 637 Times in 352 Posts
Default

Quote:
Originally Posted by Beastie View Post
Instead of decoding URL hex codes manually or relying on JavaScript-based online decoders, you can use the following one liner:
Code:
#!/bin/sh

echo "$1" | sed 's/%24/$/g;s/%26/\&/g;s/%2B/+/g;s/%2C/,/g;s/%2F/\//g;s/%3A/:/g;s/%3B/\;/g;s/%3D/=/g;s/%3F/?/g;s/%40/@/g;'
Example:
Code:
urldec.sh 'http%3A%2F%2Fforums.freebsd.org%2Fshowthread.php%3Ft%3D737'
(sorry for the second code tag, but the URL is not displayed properly in a cmd tag)
You may want to decode all possible characters:
http://sourceforge.net/apps/trac/shw...b/shweb_urldec

Code:
s/%25/%/g
s/%20/ /g
s/%09/	/g
s/%21/!/g
s/%22/"/g
s/%23/#/g
s/%24/\$/g
s/%26/\&/g
s/%27/'/g
s/%28/(/g
s/%29/)/g
s/%2a/\*/g
s/%2b/+/g
s/%2c/,/g
s/%2d/-/g
s/%2e/\./g
s/%2f/\//g
s/%3a/:/g
s/%3b/;/g
s/%3e/>/g
s/%3f/?/g
s/%40/@/g
s/%5b/\[/g
s/%5c/\\/g
s/%5d/\]/g
s/%5e/\^/g
s/%5f/_/g
s/%60/`/g
s/%7b/{/g
s/%7c/|/g
s/%7d/}/g
s/%7e/~/g
__________________
Religions, worst damnation of mankind.
"FreeBSD has always been the operating system that GNU/Linux should have been." Frank Pohlmann, IBM
http://vermaden.blogspot.com
Reply With Quote
  #200  
Old March 9th, 2013, 16:19
graudeejs's Avatar
graudeejs graudeejs is offline
Style(9) Addict
 
Join Date: Nov 2008
Location: Riga, Latvia
Posts: 4,530
Thanks: 424
Thanked 612 Times in 479 Posts
Default

Quote:
Originally Posted by vermaden View Post
You may want to decode all possible characters:
http://sourceforge.net/apps/trac/shw...b/shweb_urldec

Code:
s/%25/%/g
s/%20/ /g
s/%09/	/g
s/%21/!/g
s/%22/"/g
s/%23/#/g
s/%24/\$/g
s/%26/\&/g
s/%27/'/g
s/%28/(/g
s/%29/)/g
s/%2a/\*/g
s/%2b/+/g
s/%2c/,/g
s/%2d/-/g
s/%2e/\./g
s/%2f/\//g
s/%3a/:/g
s/%3b/;/g
s/%3e/>/g
s/%3f/?/g
s/%40/@/g
s/%5b/\[/g
s/%5c/\\/g
s/%5d/\]/g
s/%5e/\^/g
s/%5f/_/g
s/%60/`/g
s/%7b/{/g
s/%7c/|/g
s/%7d/}/g
s/%7e/~/g
These are not All possible characters (this is small and very common subset of characters)
Reply With Quote
Reply

Tags
kiss, script

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:59.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0