I have a script which takes input for two variables from users. Then, I try to use those variables in a command which is in turn being fed into a condition in an if. However, the user input value is not replacing my variables and as a result, my command is not working -  # Get acl and dc values echo "Type the ACL name:" read acl echo "Type the DATACENTER name" read dc # see if sw-list-cisco file exists in ~/$acl directory" if [ ` ls -1 ~/$acl/sw-list-cisco &gt; /dev/null | wc -l ` -gt 0 ]; then     echo "Going to do something" else      echo "no match !!!!!!!!!!!!!" fi   The if [ ` ls -1 ~/"$acl"/sw-list-cisco > /dev/null | wc -l ` -gt 0 ]; condition doesn't seem to be replacing $acl with the user input value. I have verified manually that sw-list-cisco exists in the ~/$acl directory.  What am I missing?
38090
I am trying to write an if statement to test whether there are any files matching a certain pattern. If there is a text file in a directory it should run a given script.  My code currently:  if [ -f /*.txt ]; then ./script fi   Please give some ideas; I only want to run the script if there is a .txt in the directory.
97576
I have this code that checks that the string contains only space and hash characters (“#”), if so it echos 'yes' if not echos 'no' string=&quot;###############         #             #         # #############         #             #         # ######### # #         #         # # #         # ### ##### # #         #   #     # # #         # # ###########         # #                     ###############&quot;                          confirm_variable=&quot;Yes&quot;  for (( i=0; i&lt;${#string}; i++ )); do     str=&quot;${string:$i:1}&quot;     if [ &quot;$str&quot; == &quot;#&quot; ] || [ &quot;$str&quot; == &quot; &quot; ] || [ &quot;$str&quot; == &quot;\n&quot; ] ;          then             continue         else             confirm_variable=&quot;No&quot;             break     fi  done echo $confirm_variable  I am not sure why this isn't working as if I make the string equal something like: string=&quot;## #### # #  #&quot;   it seems to work fine.
22243
I am having difficulty getting a for loop to take user input in the form of an argument to a function I created. #!/bin/bash number1=&quot;$1&quot;    #Assume user input 5 loops=&quot;$2&quot;      #Assume user input 5 if [[ ${number1} &amp;&amp; ${loops} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..${loops}}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  This script does not run when the user inputs an integer for the variable &quot;loops&quot;, and any number for &quot;number1&quot;. number1 = 5 in this case. It returns the error:  (standard_in) 1: syntax error 5 * {1..5} = /home/$USER/For_Loop_Multiplication.sh: line 9: test: {1..5}: integer expression expected /home/$USER/For_Loop_Multiplication.sh: line 11: test: {1..5}: integer expression expected  If I instead directly input an end bound on the number of loops directly into the script like the following, and number1 = 5, it runs fine: #!/bin/bash number1=&quot;$1&quot;     #Assume user input 5 if [[ ${number1} =~ ^-?[0.00-9.99]+$ ]];then    for number2 in {1..5}    do        Product=$(echo &quot;${number1} * ${number2}&quot; | bc -l)        echo &quot;${number1} * ${number2} = ${Product}&quot;        if test ${number2} -eq 7;then            { echo &quot;----------------------&quot;; echo &quot;${number2} is the best number.&quot;; echo &quot;----------------------&quot;; continue; }        elif test ${number2} -eq 11;then            { echo &quot;------------------------------&quot;; echo &quot;${number2} is pretty awesome as well.&quot;; echo &quot;------------------------------&quot;; continue; }            fi     done else    { echo &quot;Please try again with a valid numerical entry.&quot;; exit 1; } fi  It returns the result:  5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25  Essentially what I think is happening is that the script seems to be placing the entire range as the value number2, which is defined as the iterator in the for loop. Since the input for the variable &quot;loops&quot; is an integer, there should be no difference between asking the user for a range and placing it in the loop, or simply setting it as a default when I write the script. However, there apparently is a difference... Any insight into solving this problem is appreciated. Thanks.
2158
This works as expected:  $ echo a b c | xargs --replace="{}" echo x "{}" y x a b c y   This does too:  $ echo a b c | xargs --max-args=1 echo x x a x b x c   But this doesn't work as expected:  $ echo a b c | xargs --max-args=1 --replace="{}" echo x "{}" y x a b c y   And neither does this:  $ echo a b c | xargs --delimiter=' ' --max-args=1 --replace="{}" echo x "{}" y x a y x b y x c  y   I expected this output:  x a y x b y x c y   As a workaround, I am using printf and two xargs, but that is ugly:  $ echo a b c | xargs printf '%s\0' | \ &gt; xargs --null --max-args=1 --replace="{}" echo x "{}" y x a y x b y x c y   Any idea why this is happening?
4564
I can write  VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}"   the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
69962
I have an application which takes as an input attributes in double quotes embedded in single quotes. Take for example this right command:  command -p 'cluster="cl1"'   In order to automate it, I created a bash file using $CLUSTER as a variable. How should be my command? In other words, what should I put instead of cl1?  Please note that, If I modified the above command, it won't be accepted. For instance: command -p "cluster=cl1" is not accepted
46724
Why when I enter this command the prompt changes to my directory?  PS1='$(pwd)'   I am using single quotes, which means no interpolation , a.k.a echo '$(pwd)' ——→ $(pwd)  Furthermore, say that we clarified why this works... why is it functioning differently from PS1=$(pwd) ? (no quotes at all)  By different I mean that if I use the quotes then the prompt will keep changing to my current directory as I navigate through the terminal. But if I don't use quotes, then the prompt will always remain the directory that I was in when I first entered the command PS1=$(pwd)  why?
1646
The following script does not behave as I would have expected.  Adding spaces around the '=' in the conditional made it perform how I wanted, but it got me thinking, what is it actually doing inside the conditional?  #!/bin/bash S1='foo' S2='bar' if [ $S1=$S2 ]; then     echo "S1('$S1') is equal to S2('$S2') fi echo $S1 echo $S2   The output is:  S1('foo') is equal to S2('bar') foo bar   The contents of S1 and S2 don't change from what they are assigned, so the = doesn't perform an assignment.
45950
I'm writing a script and I would like to pass the results from bc into a variable. I've declared 2 variables (var1 and var2) and have given them values.  In my script I want to pass the results from bc into another variable say var3 so that I can work with var3 for other calculations.  So far I have been able write the result to a file which is not what I'm looking for and also I've been able to echo the result in the terminal but I just want to pass the result to a variable at moment so that I can work with that variable.  echo "scale=2;$var1/var2" | bc
8360
I would like to do the following: $find = &quot;start (.*) end&quot;; $replace = &quot;foo \1 bar&quot;;  $var = &quot;start middle end&quot;; $var =~ s/$find/$replace/;  I would expect $var to contain &quot;foo middle bar&quot;, but it does not work. Neither does: $replace = 'foo \1 bar';  Somehow I am missing something regarding the escaping.
60594
I want to try simple script  flag=false while !$flag do    read x    if [ "$x" -eq "true" ]    then      flag=true    fi    echo "${x} : ${flag}" done   But when I run it, if I type true, I will see that x="true" and flag="true", but the cycle doesn't end. What is wrong with the script? How can I properly invert a boolean variable?
24174
I'm trying to create an interactive bash script where I can collect user input variables to be used as arguments in another command. Can someone provide me an example of this?  Currently i want to do:   what type of content you want to delete? what is the status?   then feed it into a mysql delete query command.
17016
The following  code is supposed to compare two strings or variables. But, I cannot do it. Here, output doesn't depend upon the changing of inputs. Can anyone correct this code?    #!/bin/sh   echo "Enter your username1:"   read username1   echo "Enter your username2:"   read useraname2   if [ "$username1"=="$username2" ]   then   echo 'Same'   else   echo 'Different'   fi
121539
Here is my script:  #!/bin/bash  echo "Digite o local em que deseja instalar o IGRAFU(pressione enter para  instalar em  ${HOME}/IGRAFO):" read caminho if test -z $caminho then caminho="${HOME}/IGRAFO" fi echo "O IGRAFU será instalado no diretório: $caminho" mkdir -pv $caminho  mv -v ./* $caminho echo "Pronto!"   At 'read caminho' I may receive from the user a path like ~/somefolder. When the script receives that kind of path both mv and mkdir won't make tilde expansion, so it will try to create a ~/somefolder and not /home/username/somefolder and therefore fail.  How do I ensure that the tilde will be converted into the HOME variable?
32522
${!FOO} performs a double substitution in bash, meaning it takes the (string) value of FOO and uses it as a variable name. zsh doesn’t support this feature.  Is there a way to make this work the same in bash and zsh?  Background:  I’ve got a list of environment variables, like  PATH MAIL EDITOR   and want to first print the variable names and afterwards their values.  This works in bash but not zsh:    for VAR in LIST do         echo $VAR         echo ${!VAR} done   It should be somehow possible “the old way” with eval, but I can’t get it to work:  for VAR in LIST do         echo $VAR         echo `eval \$$VAR` done   I’m never going to understand why I can’t simply do arbitrary deep substitutions like ${${VAR}} or even ${${${VAR}}} if need be, so an explanation for that would be nice, too.
108149
I want to use a variable from the main shell in a sub shell. I did:  export mysql_root_password="test" (     echo $mysql_root_password ) | dialog --gauge "Working hard..." 6 50   There is no output. Of course there is not since I send the output to dialog. Is there a way to output specific variables still the normal way without removing the dialog part?
33483
Preface: Every couple of days there comes a question of this type, which is easy to solve with sed, but takes time to explain. I'm writing this question and answer, so I can later refer to this generic solution and only explain the adaption for the specific case. Feel free to contribute.  I have files with variable definitions. Variables consist of uppercase letters or underscore _ and their values follow after the :=. The values can contain other variables. This is Gnom.def:  NAME:=Gnom FULL_NAME:=$FIRST_NAME $NAME FIRST_NAME:=Sman STREET:=Mainstreet 42 TOWN:=Nowhere BIRTHDAY:=May 1st, 1999   Then there is another file form.txt with a template form:  $NAME Full name: $FULL_NAME Address: $STREET in $TOWN Birthday: $BIRTHDAY Don't be confused by $NAMES   Now I want a script which replaces the variables (marked with $ and the identifier) in the form by the definitions in the other file, recursively, if necessary, so I get this text back:  Gnom Full name: Sman Gnom Address: Mainstreet 42 in Nowhere Birthday: May 1st, 1999 Don't be confused by $NAMES   The last line is to ensure that no substrings of variables get replaced accidentally.