BASH SCRIPT of LS command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH SCRIPT of LS command
# 1  
Old 09-27-2012
BASH SCRIPT of LS command

I need help in writing a BASH SCRIPT of ls command.

for example:
$ ./do_ls.sh files
f1.txt
f2.jpeg
f3.doc

$ ./do_ls.sh dirs
folder1
folder2
folder3

My attempt:
Code:
#!/bin/bash
#
if test $# -d file
then
	echo $dirs
	else
if test $# -f file
then
	echo $files
	exit 1

thanks.
# 2  
Old 09-27-2012
try something like this..

Code:
#!/bin/bash
#
var=$1
if [[ "$var" == "files" ]]
then
ls -l| sed -n '/^-/p'
elif [[ "$var" == "dirs" ]]
then
ls -l| sed -n '/^d/p'
else
echo "wrong choice"
fi

# 3  
Old 09-27-2012
Code:
#!/bin/bash
if [[ $1 == "files" ]]
then
 ls -p|sed -n '/\/$/!p'
elif [[ $1 == "dirs" ]]
then
 ls -p|sed -n 's:/$::p'
fi

# 4  
Old 09-27-2012
Thanks for the reply.

I found this in my old book as a subsitue for ls command.

Code:
#!/bin/bash
#
for f in * ; do echo $f ; done

But here, I want to display only files when $ ./do_ls.sh files is entered.

and dir when $ ./do_ls.sh dirs is entered.

---------- Post updated at 02:23 PM ---------- Previous update was at 12:17 PM ----------

bump..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command works interactively but not in bash script

The below command works in the terminal interactively but not as part of a bash script. I though maybe I needed to escape the "$dir" so it isn't interpreted literally, but that's not it. Thank you :). interactively in terminal dir=/path/to new=$(ls "$dir"/*.csv -tr | tail -n 1) && echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Building JSON command with bash script

Hello. I'm new to bash script and I'm learning the basics by writing some scripts. Recently a friend of mine asked me if I could try to write a script to him to automate a couple of processes that uses JSON RPCs. I'll try to explain in few words the workflow just to contextualize the problem.... (48 Replies)
Discussion started by: psysc0rpi0n
48 Replies

3. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies

4. Shell Programming and Scripting

Bash script with awk command ERROR

Hello im new here... Im trying to read file and create folders from words in it but i get this for loop error awk : line 3 : syntax error at or near for my code is.. #!/bin/bash begin for (( i=1;i<=5;i++)); do awk -v i=$i $0 { print $i } mkdir $i done {print $i} end {} i have... (7 Replies)
Discussion started by: boxstep
7 Replies

5. Shell Programming and Scripting

How to ignore error in command in bash script?

Hello, i have bash script where im cycling some command for different lines in external file. example: while read domain;do nslookupout=$(nslookup -type=ns $domain) || true another commands done < filenamewithdomains i added: || true after the command in belief it will just skip... (6 Replies)
Discussion started by: postcd
6 Replies

6. Shell Programming and Scripting

Bash script set command to a variable

Hi, Will following set up work in bash script? I've got errors if assigning following binary command to a variable. But on the other hand, COMMAND="ls" works. Any explanation please? How can I assign binary command to a variable COMMAND then I can just call ${COMMAND}? COMMAND="rsync"... (3 Replies)
Discussion started by: hce
3 Replies

7. Shell Programming and Scripting

How to call a bash command from within a perl script?

In a bash script, one can call a perl command in the following manner, where "myperlcommand" is a perl command. perl -e 'myperlcommand(arguments)' perl -e 'print("UUUU"x4)' Now, how can one call a bash command from within a perl script? (Suppose that mybashcommand is a bash... (1 Reply)
Discussion started by: LessNux
1 Replies

8. Shell Programming and Scripting

Calling bash command in perl script

Hi, I have tried several times but failed, I need to call this script from the perl script. This one line script will be sent to /var/tmp/error Bash command: /usr/openv/netbackup/bin/admincmd/bperror -backstat -l -hoursago 12 |awk '{ print $19, $12, $14, $16}'|grep -vi default|sort >... (12 Replies)
Discussion started by: sQew
12 Replies

9. Shell Programming and Scripting

Piping output from a command into bash script

Hi all. I am using procmail to deliver an email to a script I am developing. Procmail delivers the email to the script on standard input. I imagine this is the same as piping input from a command into the script. Hence I've been testing my script by running echo 'test' | sms-autosend-backup.sh ... (2 Replies)
Discussion started by: akindo
2 Replies

10. UNIX for Dummies Questions & Answers

Cygwin bash script and read command

Hello everyone, I am struggling a bit with a batch script that I need to run in cygwin. I work in winXP and I had to write some awk scripts to do some file manipulation, and now I would like to automate the process by just running a batch file so that my colleagues can use it easily. Now, the... (2 Replies)
Discussion started by: Teroc
2 Replies
Login or Register to Ask a Question