0

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?

8
  • 1
    IMHO experts never advise to parse output of ls command. Please do add more information in your question that what is your ultimate goal which you are trying to achieve by this script. Commented Sep 16, 2020 at 7:11
  • 2
    You have piped the output of ls command to column command. Please see man column for more details on that command. Commented Sep 16, 2020 at 7:19
  • 2
    column is an external command (see man column) and that's where the error is coming from. Commented Sep 16, 2020 at 7:19
  • awk -v column="$1" '{print $column}'?? Commented Sep 16, 2020 at 7:24
  • 2
    Please don't learn awk from that page. Some very good references can be found here. The page awk one-liners explained is a very good start! Commented Sep 16, 2020 at 7:35

1 Answer 1

4

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}'
Sign up to request clarification or add additional context in comments.

5 Comments

But then, how the linked page able to run it directly as ls -l | column 3?
Note you will also need to make your script executable with chmod +x column.sh. Only necessary one time.
@anir Apparently the sentence Only one problem: the script doesn't work extends beyond it's inteded range...
@anir the page you reference is wrong, you have to call it with 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 @MarkSetchell
how 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.