question about testing in shell programming


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting question about testing in shell programming
# 1  
Old 04-08-2008
Java question about testing in shell programming

In folder A i have a file "a' and text file named infile00.I would like to do redirection :a<infile01.
There is a code to do this
Code:
#get a file "a" in /home/A
for file in `ls /home/A`
do
     if [ -x file ]
     then
         #printing out if file is an execute file
         echo $file "is an execute file"
         # get an input file named input00 in /home/A
          for input in `ls /home/A`
          do
                 if [ ! -x input ]
                 then
                         home/A/$file<input
                 fi
           done
      fi
done

When i executed the script above, output was
Code:
correct is an execute
infile00 is an execute

I tried to correct the problem but.....the result did not change at all (even got worse)
Can anyone help me out.
# 2  
Old 04-08-2008
To refer to the value of a variable, use $file with a dollar sign. (When you assign to it, there should be no dollar sign.)

You lack a slash in the innermost conditional, you want /home/A not home/A

As a matter of programming idiom, the correct way to loop over the files in a directory is simply

Code:
for file in /home/A/*

This includes the path name in the value of $file; this may or may not be what you want. To prune it off, use ${file#/home/A/}

Last edited by era; 04-08-2008 at 04:53 AM.. Reason: Remark about including and pruning directory name
# 3  
Old 04-09-2008
Quote:
Originally Posted by era
To prune it off, use ${file#/home/A/}
What does it (i.e) "${file#/home/A/}" actually do? I tried with my home directory, but the variable 'file' shows empty.
What is the meaning of 'prune it off'?
# 4  
Old 04-09-2008
Read the documentation for your shell. This construct removes the leading string after the # from the value, if present.
# 5  
Old 04-09-2008
Quote:
Originally Posted by era
Read the documentation for your shell. This construct removes the leading string after the # from the value, if present.
Suppose, the 'file' variable contains the following data:
file=/home/A/cppfiles/src

giving the command:
echo ${file#/home/A/} ==> cppfiles/src

do you mean this? Is this called "prune it off"?
# 6  
Old 04-09-2008
File 'cmd' (executable)
Code:
#!/bin/ksh
# Get an executable file in $*

list_dir=$(ls $*)

for file in $list_dir
do
     if [ -x $file ]
     then
         #printing out if file is an execute file

         echo "File $file is an execute file"

         # get a non-executable input file in $1

          for zfile in $list_dir
          do
                 if [ ! -x $zfile ]
                 then
                        echo "File $zfile is a data file"
                        $file < $zfile
                 fi
           done
      fi
done

File 'a' (executable)
Code:
#!/bin/ksh

echo "Hello, I'm the 'a' file !"
echo "Here is my input:"

while read n
do
        echo $n
done

echo "End of file 'a'"

File 'input00' (non executable)
Code:
- this
- is
- the
- input
- of
- the
- file
- intput00

File 'input01' (non executable)
Code:
- this
- is
- the
- input
- of
- the
- file
- intput01

Execute:
Code:
./cmd A/*
File A/a is an execute file
File A/input00 is a data file
Hello, I'm the 'a' file !
Here is my input:
- this
- is
- the
- input
- of
- the
- file
- intput00
End of file 'a'
File A/input01 is a data file
Hello, I'm the 'a' file !
Here is my input:
- this
- is
- the
- input
- of
- the
- file
- intput01
End of file 'a'

Smilie
# 7  
Old 04-09-2008
Quote:
Originally Posted by royalibrahim
Suppose, the 'file' variable contains the following data:
file=/home/A/cppfiles/src

giving the command:
echo ${file#/home/A/} ==> cppfiles/src

do you mean this? Is this called "prune it off"?
It's not like that's the formal term for this, but yes, that's what I meant.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Plz Help Me in This question in Shell Programming

2- Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is given, the script should count and report the number of files in this directory. Only regular files should be counted, not... (1 Reply)
Discussion started by: tahseen_22334
1 Replies

2. Shell Programming and Scripting

Plz Help Me in This question in Shell Programming

2- Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is given, the script should count and report the number of files in this directory. Only regular files should be counted, not... (1 Reply)
Discussion started by: tahseen_22334
1 Replies

3. Shell Programming and Scripting

Beginner Shell Programming Question

Hello all, I am currently try to learn the linux operating system as well as some bash programming. I have come across some online course work which has been very helpful, I have been working through some assignments and since I have no teacher to ask I have come to you experts. So the... (6 Replies)
Discussion started by: g2axiom
6 Replies

4. Shell Programming and Scripting

question about testing in shell programming(modifications were made)

In folder A i have a file "a' and text file named infile00.I would like to do redirection :a<infile01. There is a code to do this #get a file "a" in /home/A for file in /home/A/* do if $file ] then #printing out if file is an execute file echo $file "is an... (2 Replies)
Discussion started by: thungmail
2 Replies

5. Shell Programming and Scripting

question about testing in shell programming

I have 2 different directories. The first has 2 sub-folders A and B. The second as 2 sub-folders C and D and script named "script".There is a code of "script" to list 2 sub-folders C and D #!/bin/sh for file in * do && echo $file done I would like to ask how can I make a change if I... (2 Replies)
Discussion started by: thungmail
2 Replies

6. Shell Programming and Scripting

question about testing in shell programming

Hi i would like to write a "script" which takes a directory as an argument and the script will output the content of a file in this directory.Here is my code #!/bin/sh #set an argument to be a specified path $1=/home/tuan/Desktop/Shell_programming/directory #check if an argument is a... (3 Replies)
Discussion started by: thungmail
3 Replies

7. Shell Programming and Scripting

question about testing in shell programming

I created a file named q2.c in /home/tuan/Desktop/Shell_programming. I coded a script named "test" to check whether the file existed or not. My code : #!/bin/sh submitDir=/home/tuan/Desktop/Shell_programming/submit if then echo "There is no q2.c" else echo "There is q2.c" fi After... (3 Replies)
Discussion started by: thungmail
3 Replies

8. Shell Programming and Scripting

question about about Shell programming

1.if 2.then 3. # save the number of args and first argument in variables. 4. num_args=$# 5. id="$1" 6. echo "$id" 7. #echo "$1" > crapfile.txt 8. echo `sed 's/\*/'\*'/g' < crapfile.txt` Above is a partial code.I would like to ask: at line 1:... (1 Reply)
Discussion started by: thungmail
1 Replies

9. Shell Programming and Scripting

Post Shell programming: Question about source a file and read data from the file

This is shell programming assignment. It needs to create a file called .std_dbrc contains STD_DBROOT=${HOME}/class/2031/Assgn3/STD_DB (which includes all my simple database files) and I am gonna use this .std_dbrc in my script file (read the data from the database files) like this: .... (3 Replies)
Discussion started by: ccwq
3 Replies

10. Shell Programming and Scripting

Question about Shell Programming

First, I'd like to know if being a shell programmer considered a "real" programmer. is it?? also, I do create a lot of shell programs which includes full scripts to create users and maintaining records. ie phone records. now, I hear the programmer has to do some cleaning up after the... (3 Replies)
Discussion started by: IMPORTANT
3 Replies
Login or Register to Ask a Question