LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-18-2019, 12:41 PM   #1
techlifehackblog
LQ Newbie
 
Registered: Sep 2019
Posts: 3

Rep: Reputation: Disabled
Bash script to accept argument and check the exist of directory inside /home using for loop


Hi Friends,

I have created below script to accept the positional parameter and base on that determine the exist of directory.

#!/bin/bash
#This directory will check the exist of directory in home directory.
directory=/home/$*
function checkd {
if [[ -d $directory ]]
then
echo "$directory is a directory"
else
echo "$directory is not a directory"
fi
}
for file in $*
do
checkd $file
done;

When I am providing the one argument with script, it is working fine.

./homedir myvijay

/home/myvijay is a directory

The problem, I need to check the exist of multiple directory inside /home but when I am giving multiple argument, below error coming.
./homedir myvijay Nancy

/home/myvijay nancy is not a directory
/home/myvijay nancy is not a directory

The reason, I need this script to check the home directory for every while doing the backup.

I tried different method but no luck, please help.

Thanks and Regards,

TechLife Hack
 
Old 09-18-2019, 12:50 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
command line args
for home dir stuff ~/
or
Code:
$HOME/arg
Code:
#!/bin/bash

echo "enter dirs to check" 

for i in $@
do
     #add short cut for /home/userNAME
	if [[ -d ~/$i ]] ; then
		echo "$i is valid"
	else
		echo "$i is not valid"
	fi
done
minus your error checking for if amount of args <= 0 ; exit or ask again.

results
Code:
$ ./ckdir Documents Downloads boo
enter dirs to check
Documents is valid
Downloads is valid
boo is not valid

Last edited by BW-userx; 09-18-2019 at 12:59 PM.
 
Old 09-18-2019, 12:56 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
firstly, use code tags
[code]
your code
here
[/code]

your script is over complicated., and just plain wrong

Code:
#!/bin/bash
BaseDir="/home"
for i in "$@"
do
    if [[ -d ${BaseDir}/${i} ]]
    then
        echo "\"${BaseDir}/${i}\" is a directory"
    else
        echo "\"${BaseDir}/${i}\" is not a directory"
    fi
done
not perfect,
really it needs more checks on the input
 
1 members found this post helpful.
Old 09-18-2019, 01:24 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
Quote:
Originally Posted by Firerat
your script is over complicated., and just plain wrong
I beg to differ, it is not overly complicated. It is simply utilizing the use of a function to accomplish just that, a function to see if dir is valid or not.

bad code is code that does not work, good code is code that does work. Optimization of code is another topic.

this is the reason for a function, to "ideally do one thing", and in the main body it calls these function(s) to do the work.

Code:
#!/bin/bash
#This directory will check the exist of directory in home directory.
#directory=
#use modern function
#function checkd {

# a more modern syntex for bash function
checkd()
{
	file=$HOME/$file
	if [[ -d $file ]]
	then
		echo "$file is a directory"
	else
		echo "$file is not a directory"
	fi
}
 
if [[ "$#" -le '0' ]] ; then
	echo "
	Give me some directories in your home
	to check, please, YO!"
else

		
	for file in "$@"
	do
		checkd $file
	done;
fi
the use of the word 'directory' or reuse of the word 'file' for a variable is arbitrary.

results
Code:
$ ./ckdir Documents bob Downloads
/home/userx/Documents is a directory
/home/userx/bob is not a directory
/home/userx/Downloads is a directory
is going to fail
Code:
BaseDir="/home"
echo $BaseDir = /home
if user adds a home dir name to evaluate it sees

if [[ -d /home/dir ] 

no user name in path so it will always fail.

Last edited by BW-userx; 09-18-2019 at 01:47 PM.
 
1 members found this post helpful.
Old 09-18-2019, 01:43 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,454

Rep: Reputation: 664Reputation: 664Reputation: 664Reputation: 664Reputation: 664Reputation: 664
The argument to your function lands in the parameter $1 within the function!
Corrections and enhancements
Code:
#!/bin/bash
#This directory will check the exist of directory in home directory.

function checkd {
directory=/home/"$1"
if [[ -d $directory ]]
then
echo "$directory is a directory"
else
echo "$directory is not a directory"
fi
}
for file in "$@"
do
checkd "$file"
done
 
1 members found this post helpful.
Old 09-18-2019, 01:51 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
Quote:
Originally Posted by MadeInGermany View Post
The argument to your function lands in the parameter $1 within the function!
Corrections and enhancements
Code:
#!/bin/bash
#This directory will check the exist of directory in home directory.

function checkd {
directory=/home/"$1"
if [[ -d $directory ]]
then
echo "$directory is a directory"
else
echo "$directory is not a directory"
fi
}
for file in "$@"
do
checkd "$file"
done
again it is still mising the user ...
Code:
#!/bin/bash
#This directory will check the exist of directory in home directory.

function checkd {
directory=/home/"$1"
if [[ -d $directory ]]
then
echo "$directory is a directory"
else
echo "$directory is not a directory"
fi
}
for file in "$@"
do
checkd "$file"
done
is
Code:
$ ./ckdir Documents Downloads
/home/Documents is not a directory
/home/Downloads is not a directory
 
Old 09-18-2019, 01:53 PM   #7
techlifehackblog
LQ Newbie
 
Registered: Sep 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you to all of your for your valuable support.
Thanks and Regards,
TechLife Hack
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
[SOLVED] Bash Script - Check Argument with REGEX mrm5102 Programming 9 04-10-2012 10:07 AM
Trying to get a script to accept a command line argument gmccammon Linux - Newbie 1 06-24-2007 09:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:20 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