LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-12-2004, 09:42 AM   #1
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Rep: Reputation: 15
Bash Scripts


Hi,

in a bash script how can i check if a file exists?
 
Old 03-12-2004, 09:49 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
Code:
if [ -f 'file' ]
then
    stuff to do if file exists
else
    stuff to do if file doesn't exist
fi
 
Old 03-12-2004, 09:51 AM   #3
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Original Poster
Rep: Reputation: 15
cool thankyou, that worked great. what about if i want to get the first char of the filename? how do you do string manipulation?
 
Old 03-12-2004, 10:10 AM   #4
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
"what about if i want to get the first char of the filename?"

echo $FILENAME | cut -c1
 
Old 03-12-2004, 10:48 AM   #5
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Original Poster
Rep: Reputation: 15
another quick question, is it possible to get return values from functions in shell scripts?
i know you can write to global variables, but is it possible todo something like this?
strLocation=GetLocation()
?

Thanks
 
Old 03-12-2004, 11:12 AM   #6
Skute
Member
 
Registered: Jul 2003
Distribution: SUSE 9.0 Pro
Posts: 45

Original Poster
Rep: Reputation: 15
ok ive since realised you get the return value in the form of $?

but is it possible to return string values? or is it only possible to return numbers? because ive tried returning a string and it breaks the script
 
Old 03-12-2004, 11:13 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
you can return codes (numbers)

func ()
{
return -99
}

to grab output

func()
{
echo hello
}
string_hello=$(func)

that's about it though.



billy
 
Old 03-12-2004, 11:17 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
Also, in ksh i think maybe bash too?

to remove the first chracter of a variable
without calling another function

you can use the ${var:#} ${var:%} syntax:

Code:
billym.primadtpdev>   file=ipedo    
billym.primadtpdev>   echo ${file#?}
pedo

(the last char?)

billym.primadtpdev>echo ${file%?}
iped
 
  


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
little help with bash scripts sniff Programming 2 10-26-2005 06:17 PM
bash scripts hoffmanyew Programming 3 08-11-2005 02:27 AM
Need Help With Bash Scripts the_woelf Linux - Software 4 06-30-2004 10:09 AM
$? in Bash scripts clinton Linux - Newbie 4 02-20-2004 12:15 PM
Bash scripts? BajaNick Programming 3 07-05-2003 11:13 PM


All times are GMT -5. The time now is 04:19 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration