[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Ruler
From: |
Val Krem |
Subject: |
Re: [Help-bash] Ruler |
Date: |
Sat, 19 Dec 2015 00:24:28 +0000 (UTC) |
Thank you very much Greg.
It is working. However, When I invoked the command (ruler) it listing the whole
file. I just want the first few rows(5-10 lines) of the file.
Is that possible to do that?
Krem
On Friday, December 18, 2015 7:16 AM, Greg Wooledge <address@hidden> wrote:
On Fri, Dec 18, 2015 at 01:03:07AM +0000, Val Krem wrote:
> I want write it in script and put in the bin so that I can call from anywhere
> to use it.
>
> >bash$ ruler filename
>
> can you please help me how to do that.
To refresh everyone's memory (I am bringing this BACK to the mailing
list where it belongs), here's the function I wrote:
ruler() {
local cols=${COLUMNS:-$(tput cols)}
local i n tmp
if (( ${#_ruler_line1} < cols )); then
n=$(( ${#_ruler_line1} / 10 ))
for ((i=n+1; i*10 <= cols; i++)); do
printf -v tmp %10d "$i"
_ruler_line1+=$tmp
done
fi
while (( ${#_ruler_line2} < cols )); do
_ruler_line2+=1234567890
done
echo "${_ruler_line1:0:cols}"
echo "${_ruler_line2:0:cols}"
cat "$@"
}
If you simply paste that into your ~/.bashrc file and then open a new
terminal (or run "exec bash" in an existing terminal) you can use it
as a command.
If you really want to convert it into a script, then open a new file
(call it "ruler", with no extension) and put this in it:
#!/usr/bin/env bash
cols=${COLUMNS:-$(tput cols)}
unset _ruler_line1 _ruler_line2
if (( ${#_ruler_line1} < cols )); then
n=$(( ${#_ruler_line1} / 10 ))
for ((i=n+1; i*10 <= cols; i++)); do
printf -v tmp %10d "$i"
_ruler_line1+=$tmp
done
fi
while (( ${#_ruler_line2} < cols )); do
_ruler_line2+=1234567890
done
echo "${_ruler_line1:0:cols}"
echo "${_ruler_line2:0:cols}"
cat "$@"
Then move or copy the file to any directory that is in your PATH variable.
Personally I would suggest that you create the directory ~/bin/ and put
the script in there. Then add :~/bin to the end of your PATH variable,
which you would do in one of your dot files. If you don't like this
approach, you can become super-user and put the script in /usr/local/bin.
In any case, wherever you end up putting the script, you need to make
it executable. So, in addition to moving/copying it, you also need to
run:
chmod +x ruler
Doing it as a function is simpler, and more efficient. Doing it as a
script in a global location like /usr/local/bin makes it available to
other users on your system, and allows it to be invoked from other
commands like find.
See also:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/DotFiles