Sponsored Content
Top Forums Shell Programming and Scripting Shell scripting newbie problem Post 302527873 by Tibor63 on Monday 6th of June 2011 01:50:18 AM
Old 06-06-2011
Quote:
Originally Posted by agama
Looks like a few of us crossed posts. Glad you figured it out, and you should have some meaningful explanation now too.
Yes, the explanation given in your post and the one above were what I needed.

+thanks to all that helped! Smilie

---------- Post updated at 10:50 PM ---------- Previous update was at 09:31 PM ----------

Quote:
Originally Posted by agama
Easiest solution, is to cd to the desired directory and then run your loop, leaving the $d/ out of the test. And don't forget to test that the directory switch worked and issuing an error if it doesn't --- there's nothing to prevent the user from putting a regular file on the command line.

Since this seems like an assignment, I'm not offering any code.
You're right, it is an assignment, but I'm actually more interested in learning than in "just" getting it done. Even though I had it working with the code I posted earlier in post #7 (changing "for i in *" to "for i in $d/*"), I still am trying to find the most efficient way to do it.

Which is why I appreciate your alternate suggestion above of cd'ing before looping. Funny thing is that I had that thought myself hours before frustration led me to post here... but for whatever reason I didn't try to implement it, or didn't think it would work. But now that you've suggested it, it reminded me I had it, so I tried it out. It is literally as easy as taking the top loop and adding 2 extra lines ("cd $1" before the loop and "cd -" after it). Grr... a 5-second solution that I couldn't find earlier after two hours racking my brains. Oh well, at least I got it working two different ways now. Smilie Thanks again!

EDIT: and you're right I should add a conditional to check it is actually a directory... will get on that soon too!

Code:
if [ $# -eq 0 ] ; then
    d=`pwd`
    for i in * ; do
        if test -d "$d"/"$i" ; then
            echo "$i:"
        fi
    done
else
    if [ $# -eq 1 ] ; then
        cd $1
        d=`pwd`
        for i in * ; do
            if test -d "$d"/"$i" ; then
                echo "$i:"
            fi
        done
        cd -        
    fi
fi

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

shell scripting newbie question

Hi all! I'm a newbie to shell scripting. I want to create a script that will store a line from a text file in a variable so I can then use it to open firefox with that text in the address bar (the text file contains a list of addresses). I have tried the following: #!/bin/sh a='sed -n 2p... (2 Replies)
Discussion started by: jazzman
2 Replies

2. Shell Programming and Scripting

Shell scripting newbie - please be gentle.

Hello Gurus, Here's my problem, I have log files that are created automatically once a day by a feature of NetBackup called Vault. I usually move these files manually to a different location so I can FTP them later, however I know that this can be automated and so here's the info: When vault... (2 Replies)
Discussion started by: charliemp3
2 Replies

3. UNIX for Dummies Questions & Answers

Shell Scripting Newbie

I'm relatively new at this scripting game, just need to learn some basic stuff for data handling. My current need is to write a script that loops through a textfile of filenames, and for each file removes the first line and re-writes that file to a new name. In fact I could do with knowing... (1 Reply)
Discussion started by: mattyjim2
1 Replies

4. Shell Programming and Scripting

Shell scripting and ls -1 problem

Hey, I'm running knoppix and I'm trying to run a shell script to change multiple lines of text in multiple files #!/bin/sh for i in 'ls-1 test' do sed 's/bob/manny/'g $i > $i.0 mv $i.0 $i done Obviously this isn't the original file, but it's on another non-networked machine. What... (7 Replies)
Discussion started by: afroCluster
7 Replies

5. Shell Programming and Scripting

Shell Scripting NEWBIE - Need Help

Could someone please recommend a very good shell scripting book for me. I would be starting a new job that would require a very good understanding of shell scripting. Please help. (3 Replies)
Discussion started by: ayoka
3 Replies

6. Shell Programming and Scripting

newbie: writing ksh shell problem

my default profile is using ksh, I tried to write a simple scripts and I had issues, below is my scripts: $ more if_num.ksh USAGE="usage: if_num.ksh" print -n "Enter two numbers: " read x y if ((x=y)) then print "You entered the same number twice." when I tried to executed the... (6 Replies)
Discussion started by: matthew00
6 Replies

7. Shell Programming and Scripting

Shell Scripting problem

Hi guys, I am a newbie to shell scripting.Please help me to accomplish this task. Its very urgent,I should create a script which will do the following: i) "cd ~joseph/ ; mkdir -p Bing/Bong ;mkdir -p Bing/Bang" and then create 15 ".txt" files with content "Bing Bang Bong" in "Bong"... (1 Reply)
Discussion started by: mahesh_raghu
1 Replies

8. Shell Programming and Scripting

Newbie Question: What is php shell scripting?

I know php is a Web Development language but what does it have to do with shell scripting. I might be wrong about php. Is there a CLI? How do I make one and how does it work? Please don't answer these if you have any books on this. Please give names of good beginner books for php shell scripting... (3 Replies)
Discussion started by: orszhak
3 Replies

9. Shell Programming and Scripting

Shell Scripting Newbie

Hi Guys, I want to create a shell script to run multiple jobs in sequence. Explaination - If I were to run each jobs individually I would have gone to folder - "abin"(where my shellscript is place) as follows cd abin abin > runappeng.sh abc001 Now, I have list of programs which are like... (8 Replies)
Discussion started by: chaits84
8 Replies

10. Shell Programming and Scripting

Shell scripting problem

Hello. I hava homework for university but i cant do it and i need a little help if someone can help me :) I have to do a linux shell script. Write a script that does the following: 1. Check if there is a directory in / home with myDir name. If not, it creates it. 2. In the directory it... (1 Reply)
Discussion started by: alex4o0o
1 Replies
PWD(1)							    BSD General Commands Manual 						    PWD(1)

NAME
pwd -- return working directory name SYNOPSIS
pwd [-LP] DESCRIPTION
The pwd utility writes the absolute pathname of the current working directory to the standard output. Some shells may provide a builtin pwd command which is similar or identical to this utility. Consult the builtin(1) manual page. The options are as follows: -L Display the logical current working directory. -P Display the physical current working directory (all symbolic links resolved). If no options are specified, the -P option is assumed. ENVIRONMENT
Environment variables used by pwd: PWD Logical current working directory. DIAGNOSTICS
The pwd utility exits 0 on success, and >0 if an error occurs. STANDARDS
The pwd utility conforms to IEEE Std 1003.1-2001 (``POSIX.1''). SEE ALSO
builtin(1), cd(1), csh(1), sh(1), getcwd(3) BUGS
In csh(1) the command dirs is always faster because it is built into that shell. However, it can give a different answer in the rare case that the current directory or a containing directory was moved after the shell descended into it. The -L option does not work unless the PWD environment variable is exported by the shell. BSD
February 4, 2002 BSD
All times are GMT -4. The time now is 01:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy