Jump to content

 Create a shell script “q8” that takes the name of a file as argument.  If no arguments are provided the command will print out the message:
q8: command q8 needs a file name as  argument
If the argument is not a file or if the file does not exist it prints out the message
q8: Argument   “…” must be a file
if the first line of the file starts with the string “#|!” it changes permissions on the file so that it is executable by all and prints out the message
Making file “…” executable by all

 

And i have this done. 

I have worked for over 2 hours on trying to fix this, but i can't find the reason or how to do it.

 

#!/bin/bash

if [[ $# == 0 ]]

then

echo "$0: command $0 needs a file name as argument"

else if [[ -a $1 || -f $1 ]]

then

head -1 $1 | cut –c1,2 > q8_tempfile

exe=”!#”

ftest=$(cat q8_tempfile)

if [[ $ftest == $exe ]]

then

chmod ugo +x

echo “Making argument $1 executable by all”

else

echo “$0: Argument $1 must be a file”

fi

 

Thank you for your help!

Brian and Chris

Link to comment
https://linustechtips.com/topic/83990-bash-script-need-help-please/
Share on other sites

Link to post
Share on other sites

1. Please put the code in code tags.

2. Unless I'm missing something you are at least missing a "fi"

I'll take a closer look later, not much time at the moment.

EDIT:

Also, indentation, please. And I think the string for exe is wrong,

it should probably be

 

exe='#!'
EDIT 2:

Alright, I think this oughta do it, at least it works on my machine

as I think it should.

#!/bin/bashif [ $# = 0 ]; then    echo "$0: command $0 needs a file name as argument"elif [ -f $1 ]; then    if [ -z $(head -1 "$1" | grep -P '^#!') ];then        echo "Shebang not present or not in correct place,"\             "not adding executable bit."    else        chmod ugo+x $1 && echo "Making argument $1 executable by all."    fielse    echo "File does not exist or is not a regular file. Exiting."fi
A few notes:
  • I'm not sure if you intended to make an if-elif ladder or nested

    if statements. For the former, in bash it's elif, not else if.

  • Your general concept would probably have worked, but it was a bit

    convoluted IMO. I'm not a bash expert though, so I'm sure my solution

    could be improved upon as well.

  • I'm not exactly sure what you intended with that else if [[ -a $1 ... ]],

    the -a flag usuall stands for "and". I googled around, and still can't

    quite make sense of what you were trying to do there. In any case, testing

    for a regular file (so, just [ -f $filename ]) should be sufficient as

    far as I know, unless I'm overlooking something (which has happened on

    occation...).

  • There is no space between ugo and the +x.
  • Proofread your code. ;)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×