Giter Site home page Giter Site logo

shelllearn's Introduction

for #!/bin/bash

Prompt the user to enter a file name to check

read -p "Enter a file name to check: " myfile

Check if the file exists and is not empty

if [ -n "$(find . -maxdepth 1 -type f -name "${myfile}" -print -quit)" ]; then ## Find the actual file with the given name pattern actual_file=$(find . -maxdepth 1 -type f -name "${myfile}" -print -quit) ## Check if the file is not empty if [ -s "$actual_file" ]; then ## Print a message indicating the file is not empty echo "$actual_file is not empty" ## Print the contents of the file echo "Contents of $actual_file:" cat "$actual_file" else ## Print a message indicating the file is empty echo "$actual_file is empty" fi else ## Print a message indicating the file does not exist echo "$myfile does not exist" fi

Line 1 Explanation:

if [ -n "$(find . -maxdepth 1 -type f -name "${myfile}" -print -quit)" ]; then [ -n "$(find . -maxdepth 1 -type f -name "${myfile}" -print -quit)" ]: Tests whether the output of the find command is non-empty. find . -maxdepth 1 -type f -name "${myfile}" -print -quit: Searches for files in the current directory (.) matching the pattern ${myfile}. -maxdepth 1: Limits the search to the current directory only. -type f: Specifies that only files should be considered (not directories). -name "${myfile}*": Matches files whose names start with ${myfile}. -print -quit: Prints the name of the first match and then quits the search. -n: Checks if the output of the find command is non-empty. Line 2 Explanation:

actual_file=$(find . -maxdepth 1 -type f -name "${myfile}" -print -quit) actual_file=: Assigns the result of the find command to the variable actual_file. $(find . -maxdepth 1 -type f -name "${myfile}" -print -quit): Executes the find command, which searches for files matching the pattern ${myfile}* and prints the name of the first match before quitting.

shelllearn's People

Contributors

veeraganeshe avatar

Watchers

 avatar

shelllearn's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.