I am a scripting noob, and having much trouble getting this script to run correctly. Upon start it should read the home directory and sort the files into a text file. Then have user enter a user name and insert it into line 1 of the same text file. Then have the date insert into the second line of the text file. Followed by a conformation message. So far it writes the the text file but creates separate files for the inputs. I am so lost... help anyone?
ls --sort=extension > directory.txt
echo "Enter you user name..."
read input
sed -n "1w a\ '$input'" directory.txt
date=`date`
sed -n "2w a\ '$date'" directory.txt
echo "The operation completed successfully."
What OS are you using? Do you care what shell is used to do this? (If so, which shell?)
What (described in English) do you want to want the output to be?
Do you want that output to be written to your terminal, or do you want it to be saved in a file? (If in a file, what is the name of that file?)
Is the following an accurate description of what you want:
Create an output file named directory.text that contains the following data:
A user's name (the script is to ask the user to supply a name and save the response as the first line in the output file).
The current date date and time should be saved as the second line in the output file.
The remainder of the output file should be a sorted list of files in the current directory sorted with the primary sort key being the file name extension.
ls --sort=extension > directory.txt
echo "Enter you user name..."
read input
printf "1i\n$input\n.\nw\nq" | ed -s directory.txt
date=`date`
printf "2i\n$date\n.\nw\nq" | ed -s directory.txt
echo "The operation completed successfully."
ed works where sed won't
Enjoy , Have fun!.
---------- Post updated 06-04-13 at 03:44 AM ---------- Previous update was 06-03-13 at 11:06 PM ----------
bbaumg02,
Here how it ll look like:
$./script
Enter you user name...
JOHN
The operation completed successfully.
$ ls -lrt
total 20
-rwxrwxr-x 1 adms adms 236 Jun 4 00:39 script
-rw-rw-r-- 1 adms adms 98 Jun 4 00:39 test1
-rw-rw-r-- 1 adms adms 144 Jun 4 00:39 test3
-rw-rw-r-- 1 adms adms 144 Jun 4 00:39 test2
-rw-rw-r-- 1 adms adms 0 Jun 4 00:40 unix.com
-rw-rw-r-- 1 adms adms 79 Jun 4 00:40 directory.txt
$
$ cat directory.txt
JOHN
Tue Jun 4 00:40:42 PDT 2013
script
test1
test2
test3
unix.com
directory.txt
$
To summarize Don Cragun's post, I'm amazed how often people do ls -l then just throw away all that extra output. The -l is optional! You're making yourself do extra work!
ls -l to looks it good/better, so the visualization effect ll make a difference than normal command output in column(1), that mixes up. Some time extra output is better. : )
Not when you throw that away, it doesn't! People use ls -l then go through strange contortions to make it look like ordinary ls. It's a very strange habit.