LinuxQuestions.org
Visit Jeremy's Blog.
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
LQ Newbie
 
Registered: Nov 2012
Posts: 19

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: 18,684

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
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 14.2 current / ArcoLinux / Void Linux
Posts: 8,818

Rep: Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838Reputation: 1838
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
Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 672

Rep: Reputation: 317Reputation: 317Reputation: 317Reputation: 317
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
LQ Newbie
 
Registered: Nov 2012
Posts: 19

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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration