# question about testing in shell programming

**URL:** <https://community.unix.com/t/question-about-testing-in-shell-programming/192191>\
**Category:** Shell Programming and Scripting\
**Created:** [April 8, 2008, 3:49am UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191 "2008-04-08T03:49:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thungmail](https://community.unix.com/letter_avatar/thungmail/32/6_22734026db12803ebb3e059622ac3534.png) [@thungmail](https://community.unix.com/u/thungmail)\
**Post date:** [April 8, 2008, 3:49am UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191/1 "2008-04-08T03:49:15Z")

</div>

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

```nohighlight
#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

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_22734026db12803ebb3e059622ac3534.png) [@era](https://community.unix.com/u/era)\
**Post date:** [April 8, 2008, 3:52am UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191/2 "2008-04-08T03:52:32Z")

</div>

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

```nohighlight
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/}

---

<div class="post-metadata">

**Author:** ![royalibrahim](https://community.unix.com/letter_avatar/royalibrahim/32/6_22734026db12803ebb3e059622ac3534.png) [@royalibrahim](https://community.unix.com/u/royalibrahim)\
**Post date:** [April 9, 2008, 2:30am UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191/3 "2008-04-09T02:30:15Z")

</div>

> [@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'?

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_22734026db12803ebb3e059622ac3534.png) [@era](https://community.unix.com/u/era)\
**Post date:** [April 9, 2008, 3:01am UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191/4 "2008-04-09T03:01:55Z")

</div>

Read the documentation for your shell. This construct removes the leading string after the # from the value, if present.

---

<div class="post-metadata">

**Author:** ![royalibrahim](https://community.unix.com/letter_avatar/royalibrahim/32/6_22734026db12803ebb3e059622ac3534.png) [@royalibrahim](https://community.unix.com/u/royalibrahim)\
**Post date:** [April 9, 2008, 9:23am UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191/5 "2008-04-09T09:23:10Z")

</div>

> [@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"?

---

<div class="post-metadata">

**Author:** ![V3l0](https://community.unix.com/letter_avatar/v3l0/32/6_22734026db12803ebb3e059622ac3534.png) [@V3l0](https://community.unix.com/u/V3l0)\
**Post date:** [April 9, 2008, 9:49am UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191/6 "2008-04-09T09:49:45Z")

</div>

File 'cmd' (executable)

```nohighlight
#!/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)

```nohighlight
#!/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)

```nohighlight
- this
- is
- the
- input
- of
- the
- file
- intput00

```

File 'input01' (non executable)

```nohighlight
- this
- is
- the
- input
- of
- the
- file
- intput01

```

Execute:

```nohighlight
./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'

```

:b:

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/6_22734026db12803ebb3e059622ac3534.png) [@era](https://community.unix.com/u/era)\
**Post date:** [April 9, 2008, 2:04pm UTC](https://community.unix.com/t/question-about-testing-in-shell-programming/192191/7 "2008-04-09T14:04:33Z")

</div>

> [@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.
