Computing.Net > Forums > Unix > shell script (first few lines mean?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

shell script (first few lines mean?

Reply to Message Icon

Name: JANET1979
Date: December 7, 2002 at 10:27:37 Pacific
OS: XP PRO
CPU/Ram: 2G/255DDRAM
Comment:

HI,
IAM A UNIX BEGINNER AND IAM A COMPLETELY LOST AS TO WHAT THIS PART OF MY SHELL SCRIPT MEANS?

(ls|while read xx
do
if [ -f $xx ] ;then

(FULL SCRIPT BELOW)

PLZ HELP,
REGARDS 'BEMUSED UNIX ROOKIE',
JANET.

#!/bin/sh

ls|while read xx
do
if [ -f $xx ] ;then
dir=$(echo $xx|awk '{print substr( $1, 1, 1)}')
if [ ! -d $dir ] ;then
mkdir $dir
fi
mv $xx $dir
fi
done



Sponsored Link
Ads by Google

Response Number 1
Name: Jimbo
Date: December 7, 2002 at 11:09:09 Pacific
Reply:

#!/bin/sh

# run the 'ls' command with each outputted
# item being read into the variable 'xx'
ls|while read xx
do

# Check if the item is a regular file
if [ -f $xx ] ;then


# extract the first letter of the file into
# variable 'dir'. example: file named test
# would set the variable $dir to 't'
dir=$(echo $xx|awk '{print substr( $1, 1, 1)}')


# if the 'dir' variable is not an existing
# directory, create it.
if [ ! -d $dir ] ;then
mkdir $dir
fi


# move the file to the directory
# example: mv test t
mv $xx $dir
fi
done


-jim



0
Reply to Message Icon

Related Posts

See More


Sed Problem Last script



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: shell script (first few lines mean?

Shell Script Question www.computing.net/answers/unix/shell-script-question/4617.html

Shell script www.computing.net/answers/unix/shell-script/4418.html

C-shell script help www.computing.net/answers/unix/cshell-script-help/6828.html