LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 07-13-2019, 03:37 PM   #1
Andrew Stallard
Member
 
Registered: Nov 2012
Posts: 39

Rep: Reputation: Disabled
Unhappy Shell scripting error: head: invalid trailing option -- +


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.

Code:
[andrew@mynewcentos ~]$ ls -l | awk '{print $9}' | head -"$k" | tail -1
Desktop
[andrew@mynewcentos ~]$ a=`ls -l | awk '{print $9}' | head -"$k" | tail -1`
[andrew@mynewcentos ~]$ echo $a
Desktop
Where I had previously set the variable k to 3. What the heck is happening here?
 
Old 07-13-2019, 04:07 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
See https://www.gnu.org/software/bash/ma...pressions.html

Code:
if [ -d "/path/to/$direct" ] 
then
    echo "$direct found." 
else
    echo "$direct not found."
fi
 
Old 07-13-2019, 07:59 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
you can use substrings too
Code:
/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
your addition was wrong.

Last edited by BW-userx; 07-13-2019 at 09:31 PM.
 
2 members found this post helpful.
Old 07-13-2019, 10:57 PM   #4
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by Andrew Stallard View Post
k=$i+1

What the heck is happening here?
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.
 
1 members found this post helpful.
Old 07-14-2019, 12:01 AM   #5
Andrew Stallard
Member
 
Registered: Nov 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
Lightbulb

Quote:
Originally Posted by BW-userx View Post
you can use substrings too
Code:
/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
your addition was wrong.
Thank You that worked!
 
Old 07-17-2019, 08:36 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
You can directly test for a directory:
Code:
read -p "Please enter a (directory) name " directory
if [[ -d "$directory" ]]
then
  echo "$directory directory is found."
fi
 
Old 07-18-2019, 02:06 AM   #7
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by MadeInGermany View Post
You can directly test for a directory:
Code:
read -p "Please enter a (directory) name " directory
if [[ -d "$directory" ]]
then
  echo "$directory directory is found."
fi
And you don't even need the double brackets for that, test also supports -d, so
if [ -d "$directory" ]
works as well.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
win32,shell code,shell programming,shell scripting? mr.cracker Linux - Newbie 4 07-12-2013 11:20 PM
RPC Error-"118.418448784591" contained trailing data?? suliman_shah Programming 0 09-17-2007 02:37 AM
Adding a trailing zero to shell-script variables? Yalla-One Linux - General 3 12-11-2005 04:04 PM
Multi-monitor Issues on RH9, Geforce 4 Ti Dual Head + TNT2 Single Head the letter b Linux - Newbie 3 12-04-2004 11:23 PM
x86 Solaris 9 XSun and Matrox G550 dual-head... one head down, one to go. finegan Solaris / OpenSolaris 4 03-11-2003 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration