Bash shell expansion help


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Bash shell expansion help

This is what I have in my directory.

Code:
$ ls
test1.txt  test2.txt  test3.txt  test4.txt  test5.txt  test_script.sh

This is what my shellscript looks like.

Code:
#!/bin/bash

for  filename in /shell_expansion/*.txt; do
           for ((i=0; i<=3; i++)); do
               echo "$filename"
           done
done

This is what happens when I run my shellscript.

Code:
./test_script.sh
/shell_expansion/*.txt
/shell_expansion/*.txt
/shell_expansion/*.txt
/shell_expansion/*.txt

I don't understand why shell expansion isn't happening on the *. Why am I getting /shell_expansion/*.txt instead of /shell_expansion/test1.txt and so on?
# 2  
The
Code:
for  filename in /shell_expansion/*.txt

is looking for files with names ending with .txt in the directory /shell_expansion/; not for files ending with that string in the current directory.

If you're looking for files in a subdirectory of your current working directory, maybe you wanted something more like:
Code:
for  filename in ./shell_expansion/*.txt

(note the added leading period) or just remove the leading slash from the pathname:
Code:
for  filename in shell_expansion/*.txt

# 3  
An alternate approach could be with: ./test_script.sh
Code:
#!/bin/bash
list_dir="." 
# list_dir is set to current pwd, 
# as you have shown your *txt files reside in the same pwd as your script...
# this actualy means, the 'cd $list_dir;' part would not be required, 
# but this would let you get the plain file names, rather than with the full path.
for filename in $(cd "$list_dir";ls *.txt | head -n4 )
do      echo "$filename"
done

Have fun

Last edited by sea; 09-18-2015 at 02:30 AM.. Reason: changed code
# 4  
Quote:
Originally Posted by Don Cragun
The
Code:
for  filename in /shell_expansion/*.txt

is looking for files with names ending with .txt in the directory /shell_expansion/; not for files ending with that string in the current directory.

If you're looking for files in a subdirectory of your current working directory, maybe you wanted something more like:
Code:
for  filename in ./shell_expansion/*.txt

(note the added leading period) or just remove the leading slash from the pathname:
Code:
for  filename in shell_expansion/*.txt

/shell_expansion/ is my current directory.

I tried it both ways with no luck. It still won't expand the *.

Code:
$ ./test_script.sh
./shell_expansion/*.txt
./shell_expansion/*.txt
./shell_expansion/*.txt
./shell_expansion/*.txt


]$ ./test_script.sh
shell_expansion/*.txt
shell_expansion/*.txt
shell_expansion/*.txt
shell_expansion/*.txt

# 5  
Are those files in the directory /shell_expansion or ./shell_expansion? In what directory are the files located?
# 6  
Not that we don't believe you, but what you're saying doesn't make sense...

Please show us the output from the commands:
Code:
pwd
ls -l /shell_expansion/*.txt $HOME/shell_expansion/*.txt ./shell_expansion/*.txt ./*.txt

(both on standard output and on standard error output).

And, just for the fun of it, please explain why you want to print the names of all of the names of files ending in the string .txt in a directory four times.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #1002
Difficulty: Medium
During her lifetime, Grace Hopper was awarded over 100 honorary degrees from universities across the world for her work in computer science.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion in awk script

Hello, I need to split a file into two of different locations by re-direction in awk. cat infle aaa 1 3 bbb 2 4 aaa 3 3 bbb 4 4 aaa 5 3 bbb 6 4 cat /storage/tmp/group_a.gtf aaa 1 3 aaa 3 3 aaa 5 3 cat /storage/tmp/group_b.gtf bbb 2 4 bbb ... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Shell Programming and Scripting

Bash : More parameter expansion and IFS

I am trying to become more fluent with the interworking of bash and minimize the number of external calls. Sample Data. This will be the response of the snmp query. SNMPv2-MIB::sysName.0 = STRING: SomeHostName SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.9.1.1745... (5 Replies)
Discussion started by: sumguy
5 Replies

3. Shell Programming and Scripting

Bash Parameter Expansion

#!/bin/bash SNMPW='/usr/bin/snmpwalk' while read h i do loc=$($SNMPW -v3 -u 'Myusername' -l authPriv -a SHA -A 'Password1' -x AES -X 'Password2' $i sysLocation.0 2>/dev/null) loc=${loc:-" is not snmpable."} loc=${loc##*: } loc=${loc//,/} echo "$i,$h,$loc" done < $1 My question is ... ... (1 Reply)
Discussion started by: sumguy
1 Replies

4. Shell Programming and Scripting

Bash Parameter Expansion

I have made the following examples that print various parameter expansions text: iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.250.070018.sac (text%.*): iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.250.070018 (text%%.*): iv-hhz-sac/hpac/hhz (text#*.): d/iv.hpac..hhz.d.2016.250.070018.sac... (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Bash variable expansion

Hello. The file /etc/fstab contains UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 noatime,discard,acl,user_xattr 1 1 I fill a variable SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 | grep noatime,discard )echo $SOME_LINE... (3 Replies)
Discussion started by: jcdole
3 Replies

6. Shell Programming and Scripting

Bash expansion

Hello. I cannot write a command without using eval. Any help is welcome Note 1 : What does the function SOMETHING has no importance. Note 2 : What does the command find has no importance. It is an expansion variable problem : where to put or or or anythings else What works (FILTRE_1... (8 Replies)
Discussion started by: jcdole
8 Replies

7. Shell Programming and Scripting

Does SH support indirect expansion like BASH?

Hello, is there a kind soul who can answer me, does the SH support double substitution known as indirect expansion similar to BASH? The syntax for bash is ${!var}. For instance in bash I can write something like this: VAR="value" REF_VAR="VAR" echo ${!REF_VAR} and get the "value"... (1 Reply)
Discussion started by: dimentiy
1 Replies

8. Shell Programming and Scripting

Bash variable delayed expansion?

i write a batch file , here is the content. dirname='date +%Y-%m-%d' mkdir dirname but it doen's work, it just create a folder named date and +%Y-%m-%d. i have tried run the command seperately in the bash prompt. after the first sentence executed , i use $dirname to watch the value of... (4 Replies)
Discussion started by: premotheus
4 Replies

9. Shell Programming and Scripting

Brace expansion problem in Bash

I have a script that takes an option for server pools to run the script against. The option is given as a comma separated list (ie, -p 201,204,301). I'm using eval and brace expansion to get those pool numbers into an array. It works fine unless only 1 pool number is given. Here's the code: ... (5 Replies)
Discussion started by: mglenney
5 Replies

10. Shell Programming and Scripting

bash - delay expansion of variable

Hello - I have a bash script which does some logging, and I'd like to include the line number of the echo statement that pipes into $LOGGER: MYPID=$$ MYNAME=`basename $0` LOGGER="/usr/bin/logger -t $MYNAME($LINENO) -p daemon.error" ... echo 'this is an entry into the log file' | $LOGGER ... (3 Replies)
Discussion started by: scandora
3 Replies

Featured Tech Videos