Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:55:20+00:00 2026-05-31T21:55:20+00:00

I want to make a bash script that works like a makefile. It would

  • 0

I want to make a bash script that works like a makefile.
It would have the options like -archive, -clean, -backup and etc.
The only requirement is that it must have the -o argument so it specifies a name.
The problem I have right now is that I don’t know how to pull out the .c files from the arguments.

For example, if I inputed
./compile.sh -o name -backup hello_world.c print.c

How would I compile this?

Here’s the code I have so far.

#!/usr/local/bin/bash

if [ $1 != '-o' ]; then
echo "ERROR -o wasn't present as first argument"
echo "HELP"
echo "BASH syntax: $ compile –o filename –clean –backup –archive -help cfilenames"
echo "WHERE:"
echo "$             Unix Prompt"
echo "comiple       Name of bash program"
echo "-o filename   Mandatory Argument"
echo "-clean        Optional and when present deletes all .o files"
echo "-backup       Optional and copies all .c and .h files into backup directory"
echo "-archive      Optional and Tars content of source directory. Then moved to backup directory"
echo "-help     Provides list of commands"
echo "cfilenames    Name of files to be compiled together"
fi
NAME=$2
shift
shift

options=$@
arguments=($options)

index=0
for argument in $options
do
    index=`expr $index + 1`
    case $argument in
      -clean) echo "clean" ;;
      -backup) echo "backup"
        mv -f *.c ~/backup
        mv -f *.c ~/backup ;;
      -archive) echo "archive"
        tar -zcvf backup.tar.gz *
        mv -f backup.tar.gz ~/backup/backup.tar.gz
        ;;
      -help) echo "help"
                echo "HELP"
                echo "BASH syntax: $ compile –o filename –clean –backup –archive -help cfilenames"
                echo "WHERE:"
                echo "$             Unix Prompt"
                echo "comiple       Name of bash program"
                echo "-o filename   Mandatory Argument"
                echo "-clean        Optional and when present deletes all .o files"
                echo "-backup       Optional and copies all .c and .h files into backup directory"
                echo "-archive      Optional and Tars content of source directory. Then moved to backup directory"
                echo "-help         Provides list of commands"
                echo "cfilenames    Name of files to be compiled together"
        ;;
esac
done
exit;

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:55:22+00:00Added an answer on May 31, 2026 at 9:55 pm

    You appear to be looking for getopts(1P), a bash built-in for parsing options. You use it as follows:

    #!/bin/bash
    while getopts "abc:" flag do
        echo "$flag" $OPTIND $OPTARG
    done
    

    Read more: http://aplawrence.com/Unix/getopts.html#ixzz1qAQ29TFW

    If you’d like to use long options, you can use getopt(1), a separate program that you can invoke from bash. It is part of linux-util, which is part of the default install for most distros, or at least part of the base packages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex command that I'd like to make a shell/bash script of.
Suppose you want to make a bash script which supports no options but acts
I want to make a bash script that echo's something into one of the
I have this bash shell-script command that causes date to run if make succeeds
I'm trying to make a bash script that's going to backup a db2 database
I have this script that works with iconv to automatically convert the encoding of
I want to make a script that takes a file path for argument, and
I am trying to make a bash script that moves a file or directory
I have a bash script I want to run every 5 minutes from cron...
I'm trying to make a bash script that will talk to a java program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.