how does set--$(ls -l filename) work?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how does set--$(ls -l filename) work?
# 1  
Old 04-13-2008
how does set--$(ls -l filename) work?

i'm not too sure how to use set--$(ls -l filename) to set values of positional parameters.

i'm supposed to write a script that accepts a directory as an optional command line parameter. and for that directory, i'm supposed to display a single line of information about each file within it: file name, file size, file owner (in that order) using set--$(ls -l filename).
# 2  
Old 04-14-2008
this is my solution:
------------- file read_dir.sh ------------------
#!/bin/sh

dirname=$1
for filename in `ls $dirname`
do
if [ -f $dirname/$filename ]
then
set -- $(ls -l $dirname/$filename)
echo -e $9 "\t" $5 "\t" $3
fi
done
exit 0
-------------------------------------------------------

in command line: $ read_dir.sh directory_name

let's try Smilie
# 3  
Old 04-14-2008
this is what i have so far, would it be right?

# !/bin/bash
# file-info script - allows user to view information on
# files in a specified directory

dirname=$1

clear

if [ $# -ge 2 ]; then
echo "Too many parameters. Usage: file-info [directory]"
exit
fi

if [ -d $1 ]; then
cd $1
echo "Processing `pwd`: "
for filename in `ls $dirname`
do
if [ -f $dirname/$filename ]; then
set -- $(ls -l $dirname/$filename)
echo -e $9 $5 $3
fi
done
cd
else
echo "$1 is not a valid directory... terminating..."
fi
# 4  
Old 04-14-2008
The construction for filename in `ls` is an abhorrence. If there are file names with spaces or other special characters in them, it won't work right. Just use for filename in "$dirname"/* instead.

In the set, of course, you need the ls because you want the long listing, not just the file name.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SCO

Set NIC correctly , but the network does not work

I'm trying to virtualize an instance of SCO Open Server 5.0.2c in VirtualBox (called VM- A) , I can not configure the network (NIC). The NIC I'm using is PCnet -FAST III (Am79C973 ) (this NIC works with VirtualBox + SCO 5.0.5M) When I add from ' Add new LAN adapter' I detects the NIC... (2 Replies)
Discussion started by: flako
2 Replies

2. UNIX for Dummies Questions & Answers

Work on multiple directories, same filename

Can this be done and if yes how? I have 10 directories that end with the name 'cuff' eg dir1-cuff , dir2-cuff All these directories have a file named xyz.gtf How do I run the same code for each directory and print an output inside the directory something like for dir in *cuff... (3 Replies)
Discussion started by: newbie83
3 Replies

3. Shell Programming and Scripting

Extract date from filename and set timestamp

I have lots of files in this format: dvgrab-2003.06.29_15-30-24.mpg The numbers represents the date and time (YYYY.MM.DD_HH-MM-SS) How can I extract the dates from the filenames, and use the dates in the file timestamp? I guess this can be done by using "find", "sed" and "touch"? Can... (6 Replies)
Discussion started by: qwerty1234
6 Replies

4. UNIX for Dummies Questions & Answers

Aliases wont work after set command

Hi, I have setup my .profile with some helpful aliases and some set commands in it. I have it as: alias gr='autorep -G' alias c='clear' alias x='exit' alias wcl='wc -l' alias l1='ls -1 "$@"' alias ll='ls -l "$@"' alias la='ls -altr "$@"' alias l='ls -ltr "$@"' alias ml='m_ls -ltr... (5 Replies)
Discussion started by: grep_me
5 Replies

5. Shell Programming and Scripting

Set the last 4 letter in the filename as a variable

Hi all, I want to set the last 4 letter in the filename as a variable. For example, i have AB1234.txt file and i need to have last 4 letter as a variable. It should be like ; get last four letter set var = 1234 How can i write this in C shell?? Thanks, zibi (3 Replies)
Discussion started by: zibi
3 Replies

6. Shell Programming and Scripting

how to create variables in loop and assign filename after set command?

Hi, does anybody knows how to manage, that the filenames are assigned to a variable in a loop afer getting them with set command in a ksh, like: set B*.txt i=1 c=$# x=$((c+1)) echo "$x" while ] ; do _ftpfile$i="$"$i echo "$_ftpfile$i" i=$((i+1)) done The first echo returns,... (2 Replies)
Discussion started by: spidermike
2 Replies

7. Shell Programming and Scripting

Substitution in a file dont work with an Array in filename

Hi. I´ve a script that should substitude the 8th line in a file called xxx.num6. The "xxx" is set by an array filled with this command: j=0 for Par in *.sys ; do Par=`echo $Par | sed 's/\(.*\).sys/\1/'` ; Par2="$Par" ; echo "${Par2}" j=$((j + 1)); done Now i try... (0 Replies)
Discussion started by: Lock3
0 Replies

8. Ubuntu

set completion-ignore-case on doesn't work in bash

completion-ignore-case option doesn't work in my version: /home/user $ echo $BASH_VERSION 3.2.48(1)-release /home/user $ ls -l * -rw-r--r-- 1 user user 0 2009-10-18 00:09 somefile -rw-r--r-- 1 user user 0 2009-10-18 00:09 Somefile /home/user $ set completion-ignore-case on But when I... (2 Replies)
Discussion started by: Sapfeer
2 Replies

9. Solaris

Can't make 'set DISPLAY' to work

Hi, I used exceed and putty in the past to HP server with no problem to make 'set DISPLAY' work on my desktop. However now I have Solaris 9 on Sun server 480 and I only have putty. I can't bring the X window (eg xclock, etc) to my desktop. I tried X11Fowarding, xhost, and so on, it doesn't... (5 Replies)
Discussion started by: jr_zhang
5 Replies

10. Shell Programming and Scripting

AWK set FILENAME via user input

I am trying to write a awk script that prompts user for input to set the FILENAME varable. I can get it set, but I think awk is not doing anything with it. this is what I have so far #!/usr/bin/nawk -f BEGIN { FILENAME = "" printf "Enter name of file to check in : " ... (2 Replies)
Discussion started by: timj123
2 Replies
Login or Register to Ask a Question