I was learning awk from here.
There it gives:
column.sh
#!/bin/sh
column="$1"
awk '{print '"$column"'}'
But then doing following gave me error:
$ ls -l | column 3
column: 3: No such file or directory
How can use column.sh as command?
I was learning awk from here.
There it gives:
column.sh
#!/bin/sh
column="$1"
awk '{print '"$column"'}'
But then doing following gave me error:
$ ls -l | column 3
column: 3: No such file or directory
How can use column.sh as command?
Question: How can use column.sh as command?
Use column.sh as a command. Obviously, column is not column.sh.
So you may:
/the/directory/with/the/script/column.sh 3
# or
cd /the/directory/with/the/script
./column.sh 3
Alternatively, you may add the path to the script to PATH environment variable. Or you may add the script to one of the paths already existing in PATH.
export PATH="$PATH:/the/directory/with/the/script"
column.sh 3
User scripts are typically installed in $HOME/bin or, you may follow the extension to xdg-user-dirs specification, use $HOME/.local/bin directory or for all users in /usr/local/bin.
Note: an update of your script should read:
#!/usr/bin/env bash
[[ "$1" =~ [^0-9] ]] || exit 1
awk -v c="$1" '{print $c}'
chmod +x column.sh. Only necessary one time.ls -l | ./column.sh 3 or ls -l | /path/to/column.sh 3 and this will only work if column.sh has the executable flag set per comment of @MarkSetchellhow the linked page able to run it directly as ls -l | column 3? Well, the site is a (good) introduction to awk, so I guess it omits (rightfully) information about how shell works. I see there is a short explanation about chmod +x in one chapter and about installing to $HOME/bin directory, but nothing more. I guess the author didn't concern himself with such details, concentrating rather on explaining awk.
lscommand. Please do add more information in your question that what is your ultimate goal which you are trying to achieve by this script.lscommand tocolumncommand. Please seeman columnfor more details on that command.columnis an external command (seeman column) and that's where the error is coming from.awk -v column="$1" '{print $column}'??awkfrom that page. Some very good references can be found here. The page awk one-liners explained is a very good start!