•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,444 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,604 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 851 | Replies: 6
![]() |
•
•
Join Date: Oct 2007
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
I am new to Daniweb and I need some help with my programming code in Unix.
I have written an if-else statement for Bash Scripting and it doesn't work. I have been told that variables would help with my code but I still don't see my program working.
I'm suppose to use a script that will check if it exists and if not, a termination message appears.
I did this:
if [ -e $x ]
then
echo "file exists"
else
echo "file doesn't exists"
I have written an if-else statement for Bash Scripting and it doesn't work. I have been told that variables would help with my code but I still don't see my program working.
I'm suppose to use a script that will check if it exists and if not, a termination message appears.
I did this:
if [ -e $x ]
then
echo "file exists"
else
echo "file doesn't exists"
•
•
Join Date: Oct 2007
Posts: 306
Reputation:
Rep Power: 2
Solved Threads: 29
Hey There,
You've pretty much got it. Just an inch away. All you need to do is complete the if-then statement with fi.
if [ -e $x ]
then
echo "file exists"
else
echo "file doesn't exists"
fi
That should do it. Hope that helps
, Mike
You've pretty much got it. Just an inch away. All you need to do is complete the if-then statement with fi.
if [ -e $x ]
then
echo "file exists"
else
echo "file doesn't exists"
fi
That should do it. Hope that helps

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
•
•
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,012
Reputation:
Rep Power: 6
Solved Threads: 68
Also check that you have right amount of spaces in if [ ]
•
•
Join Date: Oct 2007
Posts: 306
Reputation:
Rep Power: 2
Solved Threads: 29
Hey There,
I'm not sure if you can change the file extension with grep or expr (just check for the different file extensions.
If you want to change the extension, sed is built-in to everything and might be the easiest thing to use:
#!/bin/ksh
filename=me.cpp
newfilename=`echo $filename|sed 's/cpp/out'`
echo "OLD $filename NEW $newfilename"
If I'm way off on my understanding of what you're looking for, just let me know
,Mike
I'm not sure if you can change the file extension with grep or expr (just check for the different file extensions.
If you want to change the extension, sed is built-in to everything and might be the easiest thing to use:
#!/bin/ksh
filename=me.cpp
newfilename=`echo $filename|sed 's/cpp/out'`
echo "OLD $filename NEW $newfilename"
If I'm way off on my understanding of what you're looking for, just let me know

,Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
And you have your very own thread on this topic. Keep the discussion there. And once again, I do not know why you are so fixated on using expr.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Display Properties Crashes! (Windows NT / 2000 / XP / 2003)
- desparate for help (Windows Software)
- Ay up DMR! Still got about:blank after doing everything! (Viruses, Spyware and other Nasties)
- Newby girl needing desparate help (Java)
- Broken Folder Icon OSX 10.04 Startup (OS X)
- IE 6 won't open pages directly, Win Explorer will (Web Browsers)
- hotmail not showing "from" in inbox (Web Browsers)
Other Threads in the Shell Scripting Forum
- Previous Thread: Regular Expressions using egrep or expr
- Next Thread: script fails when run via cron



Linear Mode