Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

BASH Shell Scripting: If, Then Statement

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-18-2013
inkjoy00 inkjoy00 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 18 April 2013, 10:27 PM EDT
Location: New York
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
BASH Shell Scripting: If, Then Statement

I'm having trouble trying to create a BASH shell script.

I want the user to input a command "cat file_name.c" and then the shell script will delete all comments "/* */" from file_name.c else exit.

So far I have this:


Code:
#!/bin/bash

read "cat file" // User will input command cat file_name.c
if [ '$file == $file.c ]; // Must be a .c file
then
  sed '/ /,/ /d' 
else
 exit // Any other file won't change
fi

I know the if statement needs work. What command can I put that will check all the files within a certain directory to see if it's a .c file? Unix or Linux Image

Thanks.
Sponsored Links
    #2  
Old Unix and Linux 04-18-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 181 Times in 178 Posts

Code:
#!/bin/bash

echo -n "Enter file name: "
read file
case $file in
  *.c)
    echo $file
    ;;
  *)
    exit
    ;;
esac

Quote:
the shell script will delete all comments

Code:
sed '/ /,/ /d'

The way you are trying to delete the comments will not work, will cause a lot of damage.
The Following User Says Thank You to hanson44 For This Useful Post:
inkjoy00 (04-18-2013)
Sponsored Links
    #3  
Old Unix and Linux 04-18-2013
inkjoy00 inkjoy00 is offline
Registered User
 
Join Date: Apr 2013
Last Activity: 18 April 2013, 10:27 PM EDT
Location: New York
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Sorry. I didn't fully write the code. It should delete all "/* */" in the file_name.c


Code:
#!/bin/bash

read "cat file" // User will input command cat file_name.c
if [ '$file == $file.c ]; // Must be a .c file
then
  sed '/*/,/\//d' 
else
 exit // Any other file won't change
fi

How come you chose to use case instead? Wouldn't an if, then statement work better?
    #4  
Old Unix and Linux 04-18-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 181 Times in 178 Posts
You are right that "if, then" is normally better. I used the case because it works well for matching a pattern, such as you are trying to match "*.c" pattern. There are other ways to do the pattern match. Here is one that seems pretty simple:

Code:
$ cat test.sh
#!/bin/bash

echo -n "Enter file name: "
read file
echo $file | grep -q "\.c$"
if [ $? -eq 0 ]; then
  echo $file
else
  exit
fi

---------------------

Code:
sed '/*/,/\//d'

I'm sorry, this will not do as intended, will not just delete comments. One thing to remember is that sed d command deletes entire lines, and /A/,/B/ indicates a range of lines to delete.

Code:
$ cat test.c
int x = 5; /* comment 1 */
int y = 3; /* comment 2 */
int z = 8; /* comment 3 */


Code:
$ sed '/*/,/\//d' test.c
(no output - everything deleted)

Sponsored Links
    #5  
Old Unix and Linux 04-22-2013
alvincorrea alvincorrea is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 17 March 2015, 12:49 PM EDT
Location: India
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
I got this from this (I was not allowed to post this URL as expected in this response ) another forum but this will suit you for sure

Code:
 sed -e 's/\/\*.*\*\///g'<test.c

will delete all the comments like /* comment */ perfectly
Sponsored Links
    #6  
Old Unix and Linux 04-22-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 181 Times in 178 Posts
You are basically right. But there are exceptions. Here are two that come to mind:

Code:
char *str = "/* Hi there, I'm a C comment */";
/*
int really_incredibly_long_variable_name_i_dont_want_my_teacher_to_see;
*/

Sponsored Links
    #7  
Old Unix and Linux 04-22-2013
alvincorrea alvincorrea is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 17 March 2015, 12:49 PM EDT
Location: India
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Ah you are right

Check this then ..this is also largely from different forums (not able to post the link ) but a little tweaked here and there

Code:
#!/usr/bin/perl
$s=join("",<>);
$s=~ s/("(\\\\|\\"|[^"])*")|(\/\*([^*]|\*(?=[^\/]))*\*\/)|(\/\/.*)/$1 /g;
printf "%s",$s;

I hope this will solve the problem , but please let me know if there are any errors in this approach
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with bash shell scripting boyboy1212 Homework & Coursework Questions 15 04-12-2011 06:55 PM
korn shell to bash - statement not working brdholman UNIX for Dummies Questions & Answers 5 10-15-2007 09:49 AM
Bash shell Scripting help keyvan Shell Programming and Scripting 0 05-15-2007 06:36 PM
Help!! bash shell scripting.. Fr0z3n999 Shell Programming and Scripting 1 12-07-2006 03:08 PM
bash shell scripting azazalis Shell Programming and Scripting 4 07-11-2006 03:22 PM



All times are GMT -4. The time now is 10:47 PM.