简体   繁体   中英

using awk inside of .sh bash script file

trying to achieve something in linux bash, and failing :(

I have this ls command that I use to get a nice output of cvs file in directory. I also use awk command to get rid of the permissions info which I do not need

ls -lrth *.csv | awk '{print $5, $6, $7, $8, $9}'

But now I wish to run this inside of the .sh script file. In the script if I write something like that, it works

LIST=$(ls -lrth *.csv)
echo $LIST

but if I add awk to it it doesn't, I'm gessing because of the parametres.

LIST=$(ls -lrth *.csv | awk '{print $5, $6, $7, $8, $9}')
echo $LIST

How to correctly write this inside executable sh file?

If you would like to run from inside a bash script you can remove the echo:

#! /bin/bash
# list_csv.sh
ls -lrth *.csv | awk '{print $5, $6, $7, $8, $9}'

I would suggest, however, you use an alias instead since you are simply trying to replace two commands with one. You can place it in your .bash_aliases file to persist between sessions, or run it from the console to use it for one session:

 alias lscsv="ls -lrth *.csv | awk '{print \$5, \$6, \$7, \$8, \$9}'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM