By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,479 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,479 IT Pros & Developers. It's quick & easy.

Shell script

P: 5
So im working on a lab for my unix class and im having some problems with this shell script we are supposed to do here is the question: Write a shell script that accepts a list of files(space delimited), and does the following 1)lists a long listing of the file
2) displays the number of lines in the file
3) displays the first 2 lines of the file
4) after listing all files, displays a friendly goodbye message.
separate the files with some dashes and make it easy to read.
heres my script:

#!/bin/bash

while [ "$1" != "" ]
do

ls -la$1
shift

wc
shift

head-n2
shift

echo goodbye
done

exit 3

now he gave an example in class thats like this:

#!/bin/bash

# This is a Comment Line
while [ "$1" != "" ]
do
ls -la $1
shift
done

exit 0

he said that this shell script is basically this but i had to add in the other commands besides the long listing. any help will be great. thanks
Feb 14 '08 #1
Share this Question
Share on Google+
1 Reply

P: 58
heres my script:

#!/bin/bash

while [ "$1" != "" ]
do

ls -la$1
shift

wc
shift

head-n2
shift

echo goodbye
done
You're on the right track. Everytime you use shift, your command-line arguments are shifted to the left. So, if the name of your script is test_script and you run the script with the following arguments test_script A B C D, $1 will be A, and if you use shift, A will be shifted out and $1 will become B, and so forth... Also, you might want to check out the man page for the wc command. I'm pretty sure you need to use an option to list the line count.
Feb 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.