0% found this document useful (0 votes)
5 views4 pages

Linux and C Practical Guide

The document provides a practical guide for Linux and C programming, featuring a simple shell script and a C program that demonstrate basic programming concepts. The shell script includes variable usage, condition checks, loops, and functions, while the C program showcases similar concepts with file existence checks and string comparisons. Both scripts serve as educational tools for understanding scripting and programming fundamentals.

Uploaded by

shrutimanval104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Linux and C Practical Guide

The document provides a practical guide for Linux and C programming, featuring a simple shell script and a C program that demonstrate basic programming concepts. The shell script includes variable usage, condition checks, loops, and functions, while the C program showcases similar concepts with file existence checks and string comparisons. Both scripts serve as educational tools for understanding scripting and programming fundamentals.

Uploaded by

shrutimanval104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Linux and C Programming Practical

Guide (Simplified)
Simple Shell Script for Cloud Shell
This script will:
- Use basic variables
- Check file and string conditions
- Perform simple control structures (if-else, loops)
- Use positional parameters
- Include a simple function

Shell Script Code:


#!/bin/bash

echo "Welcome to Cloud Shell!"

my_var="Cloud Shell scripting"


echo "Variable content: $my_var"

echo "Enter a number:"


read number

if [ $number -gt 0 ]; then


echo "The number is positive."
elif [ $number -lt 0 ]; then
echo "The number is negative."
else
echo "The number is zero."
fi

echo "Counting from 1 to 3:"


for i in {1..3}
do
echo $i
done

str1="hello"
str2="Hello"
if [ "$str1" = "$str2" ]; then
echo "The strings are the same."
else
echo "The strings are different."
fi

echo "Enter a file name to check:"


read filename
if [ -e "$filename" ]; then
echo "File exists."
else
echo "File does not exist."
fi

echo "The first argument passed is: $1"


echo "The second argument passed is: $2"

my_function() {
echo "This is a simple function!"
}
my_function

Simple C Program Demonstrating Shell Concepts


This C program demonstrates:
1. Variable usage
2. Decision making (if-else)
3. Loops (for, while)
4. String comparison
5. File existence check
6. Functions

C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fileExists(const char *filename) {


FILE *file = fopen(filename, "r");
if (file) {
fclose(file);
return 1;
} else {
return 0;
}
}

int main() {
char myVar[] = "C programming in action!";
printf("Variable content: %s\n", myVar);

int number;
printf("Enter a number: ");
scanf("%d", &number);

if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}

printf("Counting from 1 to 3:\n");


for (int i = 1; i <= 3; i++) {
printf("%d\n", i);
}

char str1[] = "hello";


char str2[] = "Hello";

if (strcmp(str1, str2) == 0) {
printf("The strings are the same.\n");
} else {
printf("The strings are different.\n");
}

char filename[100];
printf("Enter a file name to check: ");
scanf("%s", filename);

if (fileExists(filename)) {
printf("File exists.\n");
} else {
printf("File does not exist.\n");
}
printf("Calling a function:\n");
printf("This is a simple function demonstration!\n");

return 0;
}

You might also like