Shell Scripts
From NetBSD Wiki
You can share your Shell scripts and/or Patches here. Please upload your files and then link them, if you want them to be able to be downloaded.
Contents |
Shell Scripts
Update Script
Author: asmodehn
Description: Automates source code / ports checkout & update & system rebuild for NetBSD from 3.0 release
Warning: This file may contain malicious code, by executing it your system may be compromised.
View or Download: sysupdate.txt.
Shell Hacks
You can share your shell hacks aswell.
Convert DOS or Windows Textfiles to Unix and back
From DOS to Unix:
$ sed 's/^M$//' < dos.txt > unix.text
OR
$ sed 's/.$//' < dos.txt > unix.text
OR
$ tr -d '\r' < dos.txt > unix.text
From Unix to DOS:
$ sed 's/$/^M/' < unix.text > dos.txt
Separate Filenames
To separate the filename from a full path, you can use the shell command basename.
$ basename /home/matthias/resize.sh resize.sh
You can cut the extension aswell:
$ basename /home/matthias/resize.sh .sh resize
This works for extensions that are known in advance. For unknown extensions try this:
$ FILENAME="/home/matthias/resize.py"
$ basename $FILENAME .`echo $FILENAME|awk -F . '{print $NF}'`
resize
OR
$ filename="/home/matthias/resize.py"
$ echo "${filename%.*}"
Note: the latter does not work with Solaris' /bin/sh.
Show the Current Command in the xterm Title Bar (zsh)
It's nice to be able to identify a terminal by the currently running command, especially if you have a few of them open. Additionally, the username, hostname and the working directory is displayed. This is no problem with a proper shell. Add this to your .zshrc:
# TitleBar setting
case $TERM in
xterm*)
preexec () {print -Pn "\e]0;%n@%m [$1] [%~]\a"}
;;
esac
It will show the xterm title "root@robowork [portupgrade -a] [/usr/ports]". Explanation: preexec() is defined. This function is called by zsh before executing a command. Terminal emulators should react accordingly.
Effective Renaming (zsh)
We want to rename following files from 'txt' to 'tex' :
%> ls *.txt bp_ccc.txt bp_xxx.txt bp_yxc.txt bp_yyy.txt
Solution using zsh feature zmv (.zshrc: "autoload zmv") :
%> zmv -W "*.txt" "*.tex" %> ls *.tex bp_ccc.tex bp_xxx.tex bp_yxc.tex bp_yyy.tex
Option W automatically converts the data of the wildcards. To reverse, leave the option W.
%> zmv '(*).tex' '$1.txt' %> ls *.txt bp_ccc.txt bp_xxx.txt bp_yxc.txt bp_yyy.txt
Now, we want to rename 'bp' to 'internshipreport' and leave the rest untouched :
%> zmv 'bp(*)' 'Internshipreport$1' %> ls *.txt Internshipreport_ccc.txt Internshipreport_xxx.txt Internshipreport_yxc.txt Internshipreport_yyy.txt
A useful example is removing blank spaces from filenames :
%> ls -1 *.mp3 01 - Track 1.mp3 02 - Track 2.mp3 ... %> zmv '* *' '$f:gs/ /_' %>ls -1 *.mp3 01_-_Track__1.mp3 02_-_Track__2.mp3
You can find help to zmv in the man page "zshcontrib".
Turning off getopt
Sometimes it is necessary to turn off the internal getopt parser. For example if you try to rename files with weird names. The option "--" turns getopt off, for the arguments that follow. Example:
#> ls -testfile #> rm '-testfile' rm: illegal option -- t #> rm "\-testfile" rm: \-testfile: No such file or directory #> echo "Getting desperate..." #> rm -- -testfile #> echo "Voila, Thank you astro ;)"
Another solution (from KDE Women Homepage - "Linux-in-a-console" tips):
#> rm ./-testfile
Use vim like less
'less' and 'more' are very basic. They does not know syntax highlighting. 'vim' can colorize almost every filetype. With following alias, you can use vim as an "extended less".
alias eless='/usr/pkg/share/vim/vim64/macros/less.sh'
When called as 'eless', vim now can be used like less (h for help). The vim commands remain functional.
vim with Line Numbering and Syntax Highlighting
To have line and row numbers in vim just add following to your .vimrc:
set ruler
For Syntax Highlighting add:
syntax on
