BASH script question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH script question
# 1  
Old 04-01-2009
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  
Old 04-01-2009
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  
Old 04-01-2009
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  
Old 04-02-2009
Glad you found but is you want your script to be a bash script it have to start with:
#!/bin/bash
;-)
# 5  
Old 04-02-2009
Quote:
Originally Posted by laurentv
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  
Old 04-02-2009
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  
Old 04-03-2009
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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

2. Shell Programming and Scripting

[BASH] Performance question - Script to STDOUT

Hello Coders Some time ago i was asking about python and bash performances, and i was told i could post the regarding code, and someone would kindly help to make it faster (if possible). If you have noted, i'm on the way to finalize, finish, stable TUI - Text(ual) User Interface. It is a... (6 Replies)
Discussion started by: sea
6 Replies

3. Shell Programming and Scripting

Question about Shebang line of Bash Script

Hello All, I was writing a Bash shell script that will be executed on both an AIX server (/usr/bin/ksh) and a SLES server (/bin/bash). The AIX server has Bash installed at "/usr/bin/bash", which is in a different dir then the SLES server. So basically I am writing the script on the SLES... (4 Replies)
Discussion started by: mrm5102
4 Replies

4. Shell Programming and Scripting

Question about writing a bash script

Hello, I want to write a bash script to delete the content after '#'. However, if '#' appears in a string with "", ignore this. For example, input file: test #delete "test #not delete" Output file: test "test #not delete" Does anyone know how to write this script? Thanks (1 Reply)
Discussion started by: jeffwang66
1 Replies

5. Shell Programming and Scripting

Question in bash script.

Hi All, I need an assistance with the issue below. I wrote big script in "bash" that automatically install an LDAP on Clients. I'd be happy to know in order to avoid duplication of entries in files, How i can define into the script, if the specific expressions already exist in the file, do... (7 Replies)
Discussion started by: Aviel.shani
7 Replies

6. Shell Programming and Scripting

Bash script - loop question

Hi Folks, I have a loop that goes through an array and the output is funky. sample: array=( 19.239.211.30 ) for i in "${array}" do echo $i iperf -c $i -P 10 -x CSV -f b -t 50 | awk 'END{print '$i',$6}' >> $file done Output: 19.239.211.30 19.2390.2110.3 8746886 seems that when... (2 Replies)
Discussion started by: nitrohuffer2001
2 Replies

7. Solaris

bash shell send script question

Hi Experts, Need your help. I am trying to send a command via ssh to about a hundred network devices. I intend to do this via a bash script something similar to the below: ssh -l user testmachine.com "show version" Obviously this will not work given the password prompt that comes... (2 Replies)
Discussion started by: marcusbrutus
2 Replies

8. Shell Programming and Scripting

bash script question

Can anybody be kind to explaing me what the lines below mean ? eval line=$line export $line ] || echo $line (2 Replies)
Discussion started by: jville
2 Replies

9. Shell Programming and Scripting

one question for bash shell script

Just one question for bash shell script. In bash script, you can use *.txt to call any files in current folder that ends with .txt, like rm *.txt will remove all txt file in current folder. My question is can you actually remember or use the file name among *.txt, I know file=*.txt will not... (9 Replies)
Discussion started by: zx1106
9 Replies

10. Shell Programming and Scripting

BASH shell script question

I want to make shell script that takes a list of host names on my network as command line arguments and displays whether the hosts are up or down, using the ping command to display the status of a host and a for loop to process all the host names. Im new to shell scripting so Im not quite sure... (3 Replies)
Discussion started by: ewarmour
3 Replies
Login or Register to Ask a Question