Why doesn't this Bash shell compile directories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why doesn't this Bash shell compile directories?
# 1  
Old 05-13-2013
Why doesn't this Bash shell compile directories?

Hello I'm having a problem trying to get this Bash shell program to function properly.

Purpose: In the current directory, more into the sub-directories and compile the .c files.

Heres my code:

Code:
#!/bin/bash
 
wd=$(pwd)
for i
do
 cd ${i}
 gcc *.c
 cd $wd
done

Then I save it as myshell

Then I use
Code:
chmod u+x myshell

to compile it.

Then I use $ myshell to run it, it runs but doesn't compile the .c files...

I'm out of ideas, the code is from the textbook so it should work.

Last edited by murphy; 05-13-2013 at 01:26 AM.. Reason: errors
# 2  
Old 05-13-2013
If you don't provide any arguments to that script, the for-loop has no list on which to iterate.

On an unrelated topic, if there is any chance of whitespace in the directory pathnames, quote wd and i when you reference them.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 05-13-2013
Thanks for the reply. I understand that I need to pass an argument now, however I'm not 100% sure how to do it.

Because I'm not sure how many directories may be in the current directory, I can't manually specify them as arguments. Reading I found the args=("$@") would this work? I tried putting it in my code but I don't know if its correct. Could anyone give me a link to a similar example please?
# 4  
Old 05-13-2013
You could use find . -type d to create a list of subdirectories of your current dir. Or use ls and check the result with e.g. [ -d ... ]
# 5  
Old 05-13-2013
A loop like this perhaps:

Code:
for DIR in ./*
do
        [ -d "$DIR" ] || continue # Ignore non-directories
        cd "$DIR" || continue # Ignore if can't cd
        gcc whatever
        cd ..
done

# 6  
Old 05-13-2013
Quote:
Originally Posted by Corona688
A loop like this perhaps:

Code:
for DIR in ./*
...

In case you were commenting on the absence of a list, a bare for i is fine, since it defaults to in "$@".

Regards,
Alister
# 7  
Old 05-13-2013
Quote:
Originally Posted by alister
In case you were commenting on the absence of a list, a bare for i is fine, since it defaults to in "$@".

Regards,
Alister
Thanks for the replys.

How would I make the argument the current working directory?

I thought
Code:
wd=$(pwd)

was the argument? The examples I have don't say anything about adding any extra code, and shoudl work, but obviously not..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Bash] passing variables to executable doesn't work

Bash version 4.4.20 / Ubuntu 16.0.4 Hello, I tried to write a script that gathers some data and passes them to an executable. The executed application answers with an error. The echo output in the script returns correct values. If I copy/paste the last echo command, it get's executed... (2 Replies)
Discussion started by: sushi2k7
2 Replies

2. UNIX for Beginners Questions & Answers

Bash diff date doesn't work

Hi everyone, I've an issue trying to soustracte two dates, e.g: d1=$(date -d "Nov 18, 2017" +%s) d2=$(date +%s) # Today we are 2017-11-16 echo "$(( (d1 - d2) / 86400 ))" Output: 1 I don't understand why it doesn't work. for me, it should give "18 - 16 = 2". Much appreciated... (1 Reply)
Discussion started by: Arnaudh78
1 Replies

3. Shell Programming and Scripting

Move directories in script.sh doesn't work

Dear All, I would like move some directories in another location. Basically, my ls -lis drwxr-xr-x 3 XXXXXXXXXXXXXXXXXXXX 4096 Feb 24 02:18 data.N701_N502.ABCDE -rw-r--r-- 1 XXXXXXXXXXXXXXXXXXXX 185865797 Feb 23 11:27 data.N701_N502.ABCDE_file1 -rw-r--r-- 1 XXXXXXXXXXXXXXXXXXXX... (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

4. OS X (Apple)

OS X 'find' nogroup/nouser doesn't traverse directories?

flamingo:~ joliver$ sudo find / -nogroup find: /dev/fd/4: No such file or directory find: /home: No such file or directory find: /Library: No such file or directory find: /net: No such file or directory find: /Network: No such file or directory find: /private: No such file or directory find:... (2 Replies)
Discussion started by: jnojr
2 Replies

5. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

6. Shell Programming and Scripting

bash script to compile multiple .c files with some options

I'm trying to write a bash script and call it "compile" such that running it allows me to compile multiple files with the options "-help," "-backup," and "-clean". I've got the code for the options written, i just can't figure out how to read the input string and then translate that into option... (5 Replies)
Discussion started by: travis.batzer
5 Replies

7. Ubuntu

set completion-ignore-case on doesn't work in bash

completion-ignore-case option doesn't work in my version: /home/user $ echo $BASH_VERSION 3.2.48(1)-release /home/user $ ls -l * -rw-r--r-- 1 user user 0 2009-10-18 00:09 somefile -rw-r--r-- 1 user user 0 2009-10-18 00:09 Somefile /home/user $ set completion-ignore-case on But when I... (2 Replies)
Discussion started by: Sapfeer
2 Replies

8. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

9. Shell Programming and Scripting

Perl cmds doesn't work in Bash shell . Plz help

Hi everyone, I have a Linux OS in my PC (older version 9). Its default shell is bash. Whenever I try to run some Perl program it throws error ! eg, if I run this simple PERL program , #!/usr/bin/perl printf "\lHello \n"; $var=3 ; printf $var; @list=(1,2,3); printf "@list";... (6 Replies)
Discussion started by: adc22
6 Replies

10. UNIX for Dummies Questions & Answers

bash pattern matching echo *[! '/' ] doesn't work

without using ls, just using echo so purely pattern matching I can say echo */ <-- lists directories but how would I match files? surely something like *!/ or * but neither work ? it seems like there isn't much that I can put in but surely i should be able to put any ascii... (1 Reply)
Discussion started by: james hanley
1 Replies
Login or Register to Ask a Question