BASH Shell Scripting: If, Then Statement


 
Thread Tools Search this Thread
# 1  
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? Smilie

Thanks.
# 2  
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.
This User Gave Thanks to hanson44 For This Post:
# 3  
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  
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)

# 5  
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
# 6  
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;
*/

# 7  
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
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #223
Difficulty: Easy
In September 2019, according to NetMarketShare, Windows had just over 87% of the global desktop market, followed by Mac OS at close to 10%, and Linux in third place at around 2%.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers. int=Server1,Server2,Server3 qa=Server4,Server5,Server6 prod=Server7,Server8,Server9 echo "Enter the Environment i.e int,qa,prod" read env case $env in int) ## Need command where all the... (9 Replies)
Discussion started by: nareshreddy443
9 Replies

2. Shell Programming and Scripting

Generate sql statement using shell scripting

Can anyone please assist me? I have attached 2 input files and one output file. I need to generate the sql update statements using the above 2 input files. if inputfile2 has 5 rows, then we should generate 5 update statements because column1 is unique. inputfile1 and inputfile2 may contain more... (10 Replies)
Discussion started by: vinus
10 Replies

3. UNIX for Dummies Questions & Answers

Learn bash shell scripting

I do not know shell scripting. But at work place, I have got an in and out shell scripting task. I just need to understand a very big script. Is there any tool in which I can place the script and it can tell me the meaning of the whole script? (3 Replies)
Discussion started by: lg123
3 Replies

4. Shell Programming and Scripting

Bash shell scripting doubt

Hello All, I am setting up a cron job, where i am calling a shell script to make few builds. I got struck at a point, need some expert inputs to proceed further. The script is categorized in 5 parts and in the last part while building software it asks for few questions like:- 1. Build mode... (4 Replies)
Discussion started by: sahil_jammu
4 Replies

5. Homework & Coursework Questions

Help with bash shell scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a bash shell script that takes as an argument a name. Then your script will ask the user for two numbers.... (15 Replies)
Discussion started by: boyboy1212
15 Replies

6. Shell Programming and Scripting

how to use regular expression in Bash Shell Scripting

Hi, Actually i have written one test.sh (shell program) in bash. Here i have a variables $a which stored the value package1. Now I want to write a regular expression inside the if command that "if $a variable contains letter p in the begining of the value package1 then it is coming true.... (5 Replies)
Discussion started by: sunitachoudhury
5 Replies

7. UNIX for Dummies Questions & Answers

korn shell to bash - statement not working

Everything else seems to be working, but this isn't. Is it the "cat..." that is wrong of the condition? Thanks. cat tc_result.txt | while read LINE do if then let "files_run += 1"; echo "inside the if loop" # save current filetype case $LINE... (5 Replies)
Discussion started by: brdholman
5 Replies

8. Shell Programming and Scripting

Bash shell Scripting help

wwww wwwwwwww wwwwwwwwwwwww (0 Replies)
Discussion started by: keyvan
0 Replies

9. Shell Programming and Scripting

Help!! bash shell scripting..

Can any1 please help me... i'm really lost in using bash shell scripting... and i got to hand this up on monday... please anyone teach me how to do this assignment... Please use basic things because i just learn the program only... thanks ... (1 Reply)
Discussion started by: Fr0z3n999
1 Replies

10. Shell Programming and Scripting

bash shell scripting

Hi, i am new to UNIX. I have couple of basic questions. 1. Is the syntax for BASH shell programming same in the LINUX and SUN SOLARIS operating systems? 2. I have to work on BASH shell programming in SUN SOLARIS operating system. I am going through the documentation from the following... (4 Replies)
Discussion started by: azazalis
4 Replies

Featured Tech Videos