Linux expand dollar sign in single quotes


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Linux expand dollar sign in single quotes
# 1  
Old 12-23-2019
Linux expand dollar sign in single quotes

I am trying to get a dollar sign variable to be expanded in single quotes. Not sure what I am doing wrong. I have tried every way I can think of.

Code:
   for i in `cat file1` 
      do 
        for j in `cat file2`
        do
         ssh $i 'systemctl is-enabled "${j}" ';
      done
    done
    
   for i in `cat file1` 
      do 
        for j in `cat file2`
        do
         ssh $i 'systemctl is-enabled "$j" ';
      done
    done
    
   for i in `cat file1` 
      do 
        for j in `cat file2`
        do
         ssh $i 'systemctl is-enabled \$j ';
      done
    done
    
   for i in `cat file1` 
      do 
        for j in `cat file2`
        do
         ssh $i 'systemctl is-enabled \\$j ';
      done
    done
    
   for i in `cat file1` 
      do 
        for j in `cat file2`
        do
         ssh $i 'systemctl is-enabled \\\$j ';
      done
    done
    
   for i in `cat file1` 
      do 
        for j in `cat file2`
        do
         ssh $i 'systemctl is-enabled \\\\$j ';
      done
    done

# 2  
Old 12-23-2019
The single quotes around the ssh parameter prevent expansion. Try using a here document -
Code:
ssh <<!
remotenode  "$j"
!

# 3  
Old 12-23-2019
Ensure dollar variable to be expanded is outside of single quotes eg:
Code:
echo 'File not found in '"$PWD"', please re-enter'

In your script you would do:
Code:
   for i in `cat file1` 
      do 
        for j in `cat file2`
        do
         ssh $i 'systemctl is-enabled '"$j";
      done
    done

These 2 Users Gave Thanks to Chubler_XL For This Post:
# 4  
Old 12-24-2019
Why single quotes anyway? They stop interpretation of the string quoted. If you want to pass something that changes, how about one of these:-
Code:
ssh $i "systemctl is-enabled ${j}";               # No quotes around the variable being passed
ssh $i "systemctl is-enabled '${j}'";             # Wraps single quotes around variable being passed, interpreted locally but not interpreted again remotely
ssh $i "systemctl is-enabled \"${j}\"";           # Wraps double quotes around variable being passed, interpreted locally but could then be interpreted again remotely if the variable is a remote variable name or subshell

The loops you have are rather badly structure too. I know that ssh will gobble up your input file with a normal while read line; do ssh user@server 'printf "Hello world\n"'; done < input_file but you can open multiple input files to help you. Would you consider:-
Code:
while read -u11 outer_variable                                            # Read from file descriptor 11
do
   while read -u12 inner_variable                                         # Read from file descriptor 12
   do
      ssh "${user}@${outer_variable}" "foo bar \"${inner_variable}\""
   done 12< "${inner_variable_input_file}"                                # Supply input file on file descriptor 12
done 11< "${outer_variable_input_file}"                                   # Supply input file on file descriptor 11

Obviously it's better to use meaningful variable names. It avoids confusion from re-using them, breaking something that calls your code but has the same names and means you can understand your code when you come back to it many years later. Using i or j might save a few characters typing, but will be worse in the long run.



I hope that these help,
Robin
These 2 Users Gave Thanks to rbatte1 For This Post:
# 5  
Old 12-27-2019
A few comments allowed?
The way you programmed your script might not be the best use of resources:

Code:
for i in `cat file1`                            # useless use of cat, may fail on spaces in file1's lines, `...` deprecated
  do 
  for j in `cat file2`                          # same as above; plus: opens, reads, closes file2 linecount1 times
    do
    ssh $i 'systemctl is-enabled "${j}" ';      # creates and deletes a remote network login (linecount1 * linecount2) times
    done
  done

How about trying, given your shell offers

- the mapfile command
- "here documents",

and using

- a while instead of a for loop
- the $(...) for "command substitution" in lieu of the `...`

reading each file only once, and connecting to each node in file1 just once (only lightly tested, YMMV):

Code:
mapfile -t j < file2
while read i
  do    ssh ${i}  <<-EOFSSH
        $(for X in ${!j[@]}
            do echo "systemctl is-enabled ${j[X]}"
            done)
        EOFSSH
   done <  file1

Please report back...


EDIT: or, if your shell offers "here strings", using "parameter expansion / Pattern substitution", try
Code:
mapfile -t j < file2
IFS=$'\n'
while read i
  do    ssh -T ${i}  <<< "${j[*]/#/systemctl is-enabled }"
  done <  file1

Don't forget to reset IFS to its original value.

Last edited by RudiC; 12-30-2019 at 04:52 AM..
These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Linux dollar sign in front of directory

I'm looking at a config file with dollar signs. What do the dollar signs mean in front of a directory? dir = ./demoCA # Where everything is kept certs = $dir/certs # Where the issued certs are kept crl_dir = $dir/crl # Where the issued crl are kept new_certs_dir = $dir/newcerts # default... (1 Reply)
Discussion started by: cokedude
1 Replies

2. AIX

Implementing PowerVM without spending a single dollar

Hi experts. I want to setup a training lab. I have a Power 5 standalone server 9110-51A (p5 510) I want to enable PowerVM on it and create two LPARs I don't have money for an HMC I know I can use IVM instead I understand IVM is part of the VIOS software TWO QUESTIONS: 1- If... (12 Replies)
Discussion started by: livehho
12 Replies

3. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

4. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

5. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

6. Shell Programming and Scripting

substitute the starting dollar sign in command with blank

Hi,, Let example cmd: $$config/all Here I want to replace or subsitute blank space and also with any other character in place of "$" sign...and also want to replace backslash (/) with forward (\)......in expect script please could any one help on this.....thank you (2 Replies)
Discussion started by: swethakast
2 Replies

7. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

8. Shell Programming and Scripting

Single quotes and double quotes

Hi guys, I have a sed line in double quotes which works fine, but I want it to be in single quotes here is the sed line sed "/abc_def/s/\'.*\'/\'\${abc_def}\'/" can some one give the equivalent to the above script in single quotes Thanks a ton (5 Replies)
Discussion started by: sol_nov
5 Replies

9. UNIX for Advanced & Expert Users

Problem: Single Sign On for linux

Hi gurus, I'd like to know your opions about Single Sign On (SSO) for linux (Debian). In my company, clients want to access to different services (FTP, HTTP, Mail, Web Applications ). I think about OpenLDAP and Proxy (Squid, Vulture) to resolve this problem but i'm not sure if they can. Are there... (0 Replies)
Discussion started by: thanhdat
0 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies
Login or Register to Ask a Question