The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-01-2009
I-1 I-1 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
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
  #2 (permalink)  
Old 04-01-2009
Hammadi dali Hammadi dali is offline
Registered User
  
 

Join Date: Feb 2009
Location: Tunisia
Posts: 16
Hi
You can simple use this command, to obtain the list of extant files and directories sorted by size

PHP Code:
ls -al sort +-temp.log 
If your OS is HP-UX, you can use:
PHP Code:
 ll sort +-temp.log 
Regards
  #3 (permalink)  
Old 04-01-2009
I-1 I-1 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
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

  #4 (permalink)  
Old 04-02-2009
laurentv laurentv is offline
Registered User
  
 

Join Date: Mar 2009
Location: Montreal, Canada
Posts: 33
Glad you found but is you want your script to be a bash script it have to start with:
#!/bin/bash
;-)
  #5 (permalink)  
Old 04-02-2009
I-1 I-1 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 26
Quote:
Originally Posted by laurentv View Post
Glad you found but is you want your script to be a bash script it have to start with:
#!/bin/bash
;-)
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 :-)
  #6 (permalink)  
Old 04-02-2009
dondav dondav is offline
Registered User
  
 

Join Date: Apr 2009
Location: Southampton, UK
Posts: 1
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.
  #7 (permalink)  
Old 04-03-2009
ghoti_ ghoti_ is offline
Registered User
  
 

Join Date: Apr 2009
Location: Toronto, Canada
Posts: 1
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"

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0