LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-18-2018, 04:22 AM   #1
someguy007
LQ Newbie
 
Registered: Mar 2018
Posts: 10

Rep: Reputation: Disabled
Script issue with bash


Hello,

I am having an issue changing ownership using bash with variables.

I am testing with Yad and everything works but cant get the bash scripting to chown $frmChgOwn

Currently running as root have tried normal user and sudo still gives same result.

How it works: the Path is absolute I am testing using /usr/
Creating a directory name of: MyDir
Assigning a Group Call Teachers
And changing Ownership to Students

Here is the script:

#!/bin/bash
#Create Application Dynamics - Frame Window and fields
frmdata=$(yad --title "Create Directory" --form --width 300 --height 300 \
--field="Path" \
--field="Directory Name" \
--field="Group Owner" \
--field="Change Owner" \
--directory \
)

#Capture and parse user INPUT from fields
frmPath=$(echo $frmdata | awk 'BEGIN {FS="|"}{ print $1}')
frmDirName=$(echo $frmdata | awk 'BEGIN {FS="|"}{ print $2}')
frmGroup=$(echo $frmdata | awk 'BEGIN {FS="|"}{ print $3}') #If left blank default will be root
frmChgOwn=$(echo $frmdata | awk 'BEGIN {FS="|"}{ print $4}') #If left blank default will be root

#Execute command with user input as a variable
mkdir -p $frmPath$frmDirName

#Logic to check for empty variable in the Group Field and error control
# -z = if variable is empty
# -n = If variable is not empty
#
if [ -z "$frmGroup" ]; then
echo "Group Empty"
elif [ -n "$frmGroup" ]; then
chgrp $frmGroup $frmPath$frmDirName
elif [ -z "$frmChgOwn" ]; then
echo "Change Owner Empty"
elif [ -n "$frmChgOwn" ]; then
chown $frmChgOwn $frmPath$frmDirName
exit 0
fi
 
Old 03-18-2018, 04:50 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I admit: I am too lazy to copy and execute your script. But insert this statement before the line:
Code:
set -x
if [ -z "$frmGroup" ]; then
and see exactly what command is executed by chown. Either the command is not executed, or is executed with parameters you did not expect.

jlinkels
 
Old 03-18-2018, 06:51 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Shouldn't frmGroup and frmChgOwn be checked independently, in separate if-then-else clauses?
 
Old 03-18-2018, 06:31 PM   #4
someguy007
LQ Newbie
 
Registered: Mar 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jlinkels View Post
I admit: I am too lazy to copy and execute your script. But insert this statement before the line:
Code:
set -x
if [ -z "$frmGroup" ]; then
and see exactly what command is executed by chown. Either the command is not executed, or is executed with parameters you did not expect.

jlinkels
Thank You

Here is the new logic and works well


#Logic to check for empty variable in the Group and Owner Field and error control
# -z = if variable is empty
# -n = If variable is not empty
#
set -x
if [ -z "$frmGroup" ]; then
echo "Group Empty"
exit 1
elif [ -n "$frmGroup" ]; then
chgrp $frmGroup $frmPath$frmDirName
fi

if [ -z "$frmChgOwn" ]; then
echo "Change Owner Empty"
exit 1
elif [ -n "$frmChgOwn" ]; then
chown $frmChgOwn $frmPath$frmDirName
fi
 
Old 03-19-2018, 02:43 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Actually the error was spotted by MadeInGermany. Somehow I am not very well able to spot errors in code unless I put a serious amount of time in. But once I see the trace of execution errors pop out. That is why I know the -x option so well.

jlinkels
 
Old 03-19-2018, 04:11 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
While it works correctly now, there is no point in having an elif with the opposite of the if condition.
A simple else means the opposite of the if condition.
And if you have an exit in the if branch you do not even need an else...
 
Old 03-19-2018, 05:00 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
and also there is no reason to use even else if there was a then .. exit 1

Last edited by pan64; 03-19-2018 at 05:20 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script issue vinodm41 Linux - General 3 01-01-2018 11:14 PM
Bash Script Issue wildbi111 Programming 5 06-17-2016 02:22 PM
[SOLVED] Bash script issue kingston Linux - Newbie 10 10-07-2015 08:04 AM
Bash script issue tibix00 Programming 22 07-21-2011 12:23 PM
[SOLVED] issue with variable in bash script angel115 Programming 4 08-21-2006 01:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration