Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write a script to check if a directory exists in another directory.
Here it is:
Code:
#!/bin/bash
echo Enter name of directory
echo
read direct
a=`ls -l | wc -l`
for i in $a
do
k=$i+1
b=`ls -l | awk '{print $9}' | head -"$k" | tail -1`
c=`ls -l | awk '{print $1}' | head -"$k" | tail -1`
if [[ $b == $direct ]]
then
if [[ $c == d* ]]
then
echo $direct directory is found.
exit 0
fi
fi
done
echo $direct directory is not found
Now, I keep getting the following error when I run this:
Code:
head: invalid trailing option -- +
Try 'head --help' for more information.
head: invalid trailing option -- +
Try 'head --help' for more information.
This has me completely stumped. I used the exact same syntax for head and tail when running the commands outside the script and it ran just fine.
/home/bob/Pictures
substring=Pictures
if [[ $c =~ "$substring" ]]
then
echo $direct directory is found.
exit 0
fi
Code:
#!/bin/bash
set -xv
read -p "Please enter a (directory) name " directory
#locate $directory
#find / -type d -iname $directory
#a=`ls -l | wc -l`
#can be written like this too
a=$(ls l | wc -l)
for i in $a
do
k=$((i+1))
b=$(ls -l | awk '{print $9}' | head -"$k" | tail -1)
c=$(ls -l | awk '{print $1}' | head -"$k" | tail -1)
if [[ $b == $directory ]]
then
if [[ $c == d* ]]
then
echo $directory directory is found.
exit 0
fi
fi
done
When you would put an "echo $k" after the assignment, you would see what is happening, the assignment is done as a string operation, NOT as a numeric addition.
So "k" is NOT increased, but set to a string with "+1" appended.
/home/bob/Pictures
substring=Pictures
if [[ $c =~ "$substring" ]]
then
echo $direct directory is found.
exit 0
fi
Code:
#!/bin/bash
set -xv
read -p "Please enter a (directory) name " directory
#locate $directory
#find / -type d -iname $directory
#a=`ls -l | wc -l`
#can be written like this too
a=$(ls l | wc -l)
for i in $a
do
k=$((i+1))
b=$(ls -l | awk '{print $9}' | head -"$k" | tail -1)
c=$(ls -l | awk '{print $1}' | head -"$k" | tail -1)
if [[ $b == $directory ]]
then
if [[ $c == d* ]]
then
echo $directory directory is found.
exit 0
fi
fi
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.