one question for bash shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting one question for bash shell script
# 1  
Old 03-04-2008
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 do this if you call $file later.

Thanks!

Last edited by zx1106; 03-05-2008 at 12:52 AM..
# 2  
Old 03-05-2008
You can use a for loop to iterate through the files

Code:
for f in *
do
    echo $i
    # do whatever you want with the file
done

# 3  
Old 03-05-2008
You can use:
Code:
file=`ls *.nex`

# 4  
Old 03-05-2008
Quote:
Originally Posted by agn
You can use a for loop to iterate through the files

Code:
for f in *
do
    echo $i
    # do whatever you want with the file
done

Thanks a lot!

I tried this method before, but how could I do if I want to change a file among *.txt to *.exe (keep the first part while change the second part)?

Thanks!

Last edited by zx1106; 03-05-2008 at 12:53 AM..
# 5  
Old 03-05-2008
i think this will help:-

read ext
#<enter the ext>
for i in `ls *.$ext`
do
echo $i
done
# 6  
Old 03-09-2008
Thanks! This certainly works, but you have to type every time. So there is probably a way you do not need to type while can finish this?
# 7  
Old 03-09-2008
Quote:
Originally Posted by danmero
You can use:
Code:
file=`ls *.nex`

not necessary to use ls
Code:
file="*.nex"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

4. 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

5. 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

6. 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

7. Shell Programming and Scripting

Quick question: calling c-shell script from bash

Hello, I have a quick reference question: I have a very long, but fairly straigtforward script written in c-shell. I was wondering if it is possible to call this script from bash (for ex. having a function in bash script which calls the c-shell script when necessary), and if so, are there any... (1 Reply)
Discussion started by: lapiduslost
1 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

BASH script question

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? ... (6 Replies)
Discussion started by: I-1
6 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