Jump to content

UNIX shell scripting


SenorDestruction

Recommended Posts

hi there, having a bit of trouble with something in a shell script, i wonder if you could help me, im using the bourne shell...

i have a script which works on a file, but i want to make sure the user hasnt entered a pathname using an if statement, i assume theres a way to use regex here, but i cant seem to get it to work...

if [ $file = ***Not sure what to enter*** ]

then echo "$file: can't be a pathname"

when using awk i know you have to use a tilde rather than an = when using regular expressions and if statements, but that doesnt work here...

thanks for your help, and if anyone has any questions of a similar nature, here would probably be a reasonable place to ask them...

Link to comment
Share on other sites

  • 1 month later...

You could do something like this. . .

if [[ $file == */]]; then

  echo whatever

fi

Then again, this will only check for a string ending with a '/'.

What if the user enters '/etc' or '/usr/bin' for example?

I'm sure you can figure out how to modify the example to check for these as well.

BTW, you should check out the Advanced Bash-Scripting Guide hxxp: tldp. org/LDP/abs/html/

it also has a section on Regular Expressions.

kdb

Link to comment
Share on other sites

-e = exists

-x = exists and is accessible

-d = exists and is a directory

-f = exists and is a file

Remember that '[' is actually a program (part of coreutils on Linux) so you need to have a space between it and whatever comes next.

Good: if [ -x "file.txt" ] ; then echo YES; fi

Bad: if [-x "file.txt" ] ; then echo YES; fi

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...