LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-21-2020, 04:16 PM   #1
witchkinkofangmar
Member
 
Registered: May 2019
Posts: 83

Rep: Reputation: Disabled
Why is variable in bash script executing as a command when I run it?


My variable:
Code:
ipaCheck=$(ipa-client-install --mkhomedir -p $ipaUser --password $ipaPass --unattended | echo $?)
Executes as a command everytime I run the script. I want it to store the command, not run it.
 
Old 07-21-2020, 04:34 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,706

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
https://www.gnu.org/software/bash/ma...stitution.html

Because that is how command substitution works.

If you want to store the command as a string then
Code:
ipaCheck="ipa-client-install --mkhomedir -p $ipaUser --password $ipaPass --unattended | echo $?"
However, without knowing what you are actually trying to accomplish I am not sure if that is what you actually need to do.
 
Old 07-21-2020, 04:46 PM   #3
witchkinkofangmar
Member
 
Registered: May 2019
Posts: 83

Original Poster
Rep: Reputation: Disabled
I just want to get the output of that command, which should be 0 or 3 if it fails and run the command without echo $? if it's not 0 or 3. So something like:

if [[ "$ipaCheck" == 0 || "$ipaCheck" == 3 ]]; then
exit 0
elif [[ "$ipaCheck" != 0 ]]; then
$ipaInstall
else
echo "Something is wrong. Please contact support."
fi
 
Old 07-21-2020, 05:00 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
You want a function:
Code:
ipaCheck() {
ipa-client-install --mkhomedir -p $ipaUser --password $ipaPass --unattended | echo $?
}
Then you can use
Code:
if [[ "$(ipaCheck)" == 0 || "$(ipaCheck)" == 3 ]]; etc.
This is just to show you how it's done, not tested, probably requires some adjustment I haven't thought of now.
 
Old 07-21-2020, 05:41 PM   #5
witchkinkofangmar
Member
 
Registered: May 2019
Posts: 83

Original Poster
Rep: Reputation: Disabled
Awesome, thanks. I'll try it out.
 
Old 07-21-2020, 06:17 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Minor nit: This command
Code:
ipa-client-install --mkhomedir -p $ipaUser --password $ipaPass --unattended | echo $?
pipes the output of ipa-client-install into the echo. This doesn't make much sense. I would replace the pipe sign with a semicolon.

Also, you say two contradicting things:
Quote:
I want it to store the command, not run it.
and
Quote:
I just want to get the output of that command
If you want the output of that command, unfortunately you need to run it.
 
Old 07-21-2020, 09:58 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,706

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
Your confusing output of a command with its exit status.
Every linux command executed by the shell script or user returns an exit status i.e a number (0-255) which is stored in the variable $?
You do not need command substitution but to get the exit status the command needs to be executed.

Code:
ipa-client-install --mkhomedir -p $ipaUser --password $ipaPass --unattended
ipacheck=$?
Code:
ipa-client-install --mkhomedir -p $ipaUser --password $ipaPass --unattended
ipacheck=$?
if [ $ipacheck -eq 0 || $ipacheck -eq 3 ]
then
   echo "All good"
else
  echo "Something is wrong"
fi

Last edited by michaelk; 07-21-2020 at 10:00 PM.
 
1 members found this post helpful.
Old 07-22-2020, 01:58 PM   #8
witchkinkofangmar
Member
 
Registered: May 2019
Posts: 83

Original Poster
Rep: Reputation: Disabled
I decided to just check if the file that gets created exists or not. This works when I curl the script and run it but it still executes the command if I curl another script that curls this script:

works: bash -c "$(curl -fsSL 10.1.10.31:80/ipaEnroll.sh)"
doesnt work: bash -c "$(curl -fsSL 10.1.10.31:80/bootstrap.sh)"

Code:
#!/Bin/bash
#bootstrap.sh
bash -c "$(curl -fsSL 10.1.10.31:80/ipaEnroll.sh)"
bash -c "$(curl -fsSL 10.1.10.31:80/reg.sh)"
Code:
ipaUser=$(echo user | base64 --decode)
ipaPass=$(echo pass | base64 --decode)
ipaDir="/etc/ipa/default.conf"

ipaInstall() {
    ipa-client-install --mkhomedir -p $ipaUser --password $ipaPass --unattended
}

if [[ -f "$ipaDir" ]]; then
    echo "Identity management is already configured."
elif [[ ! -f "$ipaDir" ]]; then
    ipaInstall
else
    echo "contact support."
fi
 
Old 07-22-2020, 06:46 PM   #9
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
What do you mean by "works" and "doesn't work"?
Quote:
Originally Posted by witchkinkofangmar View Post
Code:
#!/Bin/bash
#bootstrap.sh
bash -c "$(curl -fsSL 10.1.10.31:80/ipaEnroll.sh)"
bash -c "$(curl -fsSL 10.1.10.31:80/reg.sh)"
Well, since /Bin/bash is unlikely to exist on your computer, I can see why this doesn't work.

EDIT:
The if-then-else in your second script can be simplified like this:
Code:
if [[ -f $ipaDir ]]
then echo already installed
else ipaInstall
fi

Last edited by berndbausch; 07-22-2020 at 06:52 PM.
 
Old 07-23-2020, 02:48 PM   #10
witchkinkofangmar
Member
 
Registered: May 2019
Posts: 83

Original Poster
Rep: Reputation: Disabled
By doesn't work, I mean that it still is executing the command from the else condition if I curl the script that curls the ipaEnroll script. If I just curl the ipaEnroll, it works fine. It's like it's completely ignoring the condition.
 
Old 07-23-2020, 03:57 PM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,706

Rep: Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898Reputation: 5898
You will need to explain your process.

I do not understand why you are using curl.

Normally a web server can not execute bash scripts directly from its document root and is considered a big security hole. So how is your system configured?

I have not examined the entire process yet but it is probably some environment thing.
 
Old 07-27-2020, 09:57 PM   #12
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by witchkinkofangmar View Post
if [[ "$ipaCheck" == 0 || "$ipaCheck" == 3 ]]; then
exit 0
you can use case
Code:
case "ipaCheck" in 0 | 3 ) exit 0 ;; esac
but this won't work
Code:
elif [[ "$ipaCheck" != 0 ]]; then
    $ipaInstall
else
    echo "Something is wrong. Please contact support."
After the first test the failure of the second would mean only that value of ipaCheck is non-numerical - in case of bash exits complaining about mismatching types and none of $ipaInstall or echo .. will be ever executed.

Last edited by igadoter; 07-27-2020 at 09:58 PM.
 
  


Reply

Tags
bash, centos7, linux, variables



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Get bc script variable into a bash script variable? 14hei Programming 6 02-26-2016 07:34 AM
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
bash script: how to assign command ouput to a variable without executing it? bostonantifan Programming 1 02-12-2011 11:55 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM
Bash script run via cron not executing MYSQL command mackstar Linux - Server 4 04-23-2009 05:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:54 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