LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2018, 01:30 PM   #1
bobsinclair
LQ Newbie
 
Registered: Oct 2018
Posts: 15

Rep: Reputation: Disabled
Creating a Bash shell Script


I am attempting to create a script to see if an argument is regular file or not so far my Script constits of the Following

#!/bin/bash

test-f 'Example' && echo Yes

If i have Echo as the File name i receive the output Yes,but if i Replace it with the name of a Directory i receive no output
 
Old 11-26-2018, 01:41 PM   #2
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Code:
help test
or "man test" will tell you more, but for now:
Code:
help test | grep -- '-d\|-f'
      -d FILE        True if file is a directory.
      -f FILE        True if file exists and is a regular file.
$
hence "echo Yes" will not be executed as test -f will return false if you point it at a directory.
 
Old 11-26-2018, 03:51 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Couple of examples:
Code:
read -p "Enter file/dir/pipe name :" f_name
Code:
if [ -f "$f_name" ]; then
    echo "It's a regular file"
elif [ -d "$f_name" ]; then
    echo "It's a directory"
elif [ -p "$f_name" ]; then
    echo "It's a named pipe"
fi
#Or
Code:
[ -f "$f_name" ] && echo "It's a regular file"
[ -d "$f_name" ] && echo "It's a directory"
[ -p "$f_name" ] && echo "It's a named pipe"
#Or
Code:
for i in -f -d -p; do
    if [ "$i" "$f_name" ]; then
        echo "$i"
    fi
done
 
Old 11-26-2018, 04:10 PM   #4
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
to use test itself, check for directory presents
Code:
#!/bin/bash

if test -d /usr; then
  echo "it is "
  else
  echo "no it isn't"
  fi
your code
Code:
#!/bin/bash

test-f 'Example' && echo Yes
test always checks for true, if it is a truth then it executes, if it is not a truth errors.

both still equate to true, being the executing condition.

if true [directory] do something, else do something else.
if !true [not directory] then do something, else do something else

if directory is there do something is a truth if it is.
if the directory is not there, then that is a truth if it is not there.

Code:
test -d /usr &&  echo "it is " ||   echo "no it isn't"

[[ -d /directory ]] && echo "it is there" || echo "it is not there"
as this is a ternary operation, BASH has no true ternary operator.

this here
Code:
userx@SlackOLatern:~/Documents$ touch Example
userx@SlackOLatern:~/Documents$ test -f 'Example' && echo "yes"
yes

userx@SlackOLatern:~/Documents$ test -f 'Example' && echo "no"
no

userx@SlackOLatern:~/Documents$ test -f 'Example' && echo "whatevr you wnat me to do"
whatevr you wnat me to do
is just you telling it to say whatever you want when the condition is true.

Last edited by BW-userx; 11-26-2018 at 04:27 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Require some help with creating a bash-shell script and a few other questions guiltycrown657 Linux - Newbie 2 12-01-2015 08:29 AM
ssh from solaris 9 with a bash shell of 2.0 to suse 3.2 bash shell slufoot80 Solaris / OpenSolaris 1 09-19-2012 01:19 PM
Creating array from command output (bash shell script) Rizla Programming 5 01-26-2011 10:47 PM
Help me in creating my first shell (bash) script. mq15 Linux - Newbie 2 07-30-2009 11:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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