![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to make your bash script run on a machine with csh and bash | npatwardhan | Shell Programming and Scripting | 3 | 11-19-2008 04:17 AM |
| dead simple bash script question | graysky | Shell Programming and Scripting | 12 | 11-12-2008 04:37 PM |
| passing variable from bash to perl from bash script | arsidh | Shell Programming and Scripting | 10 | 06-04-2008 01:25 PM |
| one question for bash shell script | zx1106 | Shell Programming and Scripting | 9 | 03-10-2008 12:40 AM |
| BASH shell script question | ewarmour | Shell Programming and Scripting | 3 | 05-24-2002 06:10 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
BASH script question / create a script that gets a filename as an argument
Hi, I want to create a script that gets a filename as an argument. The script should generate a listing in long list format of the current directory, sorted by file size. This list must be written to a new file by the filename given on the command line. Can someone help me with this? Here is my start: Code:
#!/bin/sh echo "Please give in the filename" read NEW_FILENAME #read the current directory in long listing and sort by filesize (small to big) and writes it to the given filename ls -l -S -r > $NEW_FILENAME But I am kind of stuck. Thanks for your help so far. Last edited by I-1; 04-01-2009 at 10:13 AM.. Reason: title unclear |
|
||||
|
Hi
You can simple use this command, to obtain the list of extant files and directories sorted by size PHP Code:
PHP Code:
|
|
||||
|
Hi, I think I allready found the solution ... Code:
#!/bin/sh echo "Please give in the filename" read NEW_FILENAME #read the current directory in long listing and sort by filesize (small to big) and writes it to the given filename ls -l -S -r > $NEW_FILENAME |
|
||||
|
I am a n00b when it comes to shell scripting ... but this is one of the things you cant miss when you read teh guides and books on this subject :-)
|
|
||||
|
Hi,
To add a command line arguement to a script try: listit.sh #!/bin/sh if test $# -gt 0; then target=$1; else echo "no output file specified using default.lst" target=default.lst fi ls -l -S -r > $target You can then run ./listit.sh myfile.lst If you just run the script without an argument it will use the default.lst file. |
|
||||
|
If you want this to behave more like a standard unix command line tool, you might want to consider using getopts. For example: Code:
#!/bin/bash
usage() {
cat <<EOT
Usage: `basename $0` [-f] [-o filename]
-o file output to filename. Default: prompt for filename
-f force overwrite if output file already exist
EOT
}
filename=""
force=false
while getopts fo: opt; do
case "$opt" in
o) filename="$OPTARG" ;;
f) force=true ;;
esac
done
shift `expr $OPTIND - 1`
if [ -z "$filename" ]; then
read -p "Enter a filename: " filename
fi
if [ -s "$filename" -a $force = "false" ]; then
echo "ERROR: $filename exists" >&2
exit 1
fi
ls -lSr > "$filename"
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|