Unable to pass password with space to shell script [duplicate] - bash

This question already has answers here:
Passing arguments with spaces between (bash) script
(1 answer)
Parsing variables from config file in Bash
(7 answers)
Closed 5 years ago.
I have below script which turns on the tether mode on my intel edison.
But when I entered password with space it shows error that argument list is long. Without space it works perfectly fine
#!/usr/bin/env bash
SSID_FILE="/usr/share/apache2/htdocs/userhotspotname.conf"
if [ -s "$SSID_FILE" ]
then
filename="/usr/share/apache2/htdocs/userhotspotname.conf"
while IFS= read -r line; do
if [[ $line == hotspotna* ]] ; then
name=$(echo $line | grep "hotspotname" | cut -d= -f2)
fi
if [[ $line == hotspotpa* ]] ; then
pass=$(echo $line | grep "hotspotpass" | cut -d= -f2)
#pass="'${pass}'"
echo $pass
fi
done < "$filename"
**connmanctl tether wifi on "${name}" ${pass}**
fi
Below is userhotspotname.conf file content
hotspotname=cvg_space
hotspotpass=cgoma pwd space
Since password is with space
connmanctl tether wifi on "${name}" ${pass}
This command is failing since it is considering it as 4 arguments.
Please help in this if anyone has idea.

Related

Create a mapping file in bash [duplicate]

I'm trying to read the information of a structured file into an associative array using Bash script. The file contains in each line the name of a person and its address, separated by a "|". For example:
person1|address of person1
person2|address of person2
...
personN|address of personN
I tried to do this using the script below. Within the WHILE loop, the information is being printed. However, in the FOR loop the information is not being printed. It seems that the information is not being stored in the associative array outside of the WHILE loop.
What am I doing wrong? Why this is not working? Is there more efficient ways to do that?
#!/bin/bash
declare -A address
cat adresses.txt | while read line
do
name=`echo $line | cut -d '|' -f 1`
add=`echo $line | cut -d '|' -f 2`
address[$name]=$add
echo "$name - ${address[$name]}"
done
for name in ${!address[*]}
do
echo "$name - ${address[$name]}"
done
Wrong and useless usage of cut
#!/bin/bash
declare -A address
while IFS=\| read name add
do
address[$name]=$add
done < adresses.txt
for name in ${!address[*]}
do
echo "$name - ${address[$name]}"
done
cat addresses.txt | while read line
do
...
done
Shell commands in a pipelines are executed in subshells. Variables set in subshells aren't visible the parent shell.
You can fix this by replacing the pipelines with a redirection.
while read line
do
...
done < addresses.txt
Extending the accepted answer to resolve the OP's comment:
#!/bin/bash
declare -A address
while IFS='|' read name add
do
address[$name]=$add
echo "$name - ${address[$name]}"
done < adresses.txt
for name in "${!address[#]}"
do
echo "$name - ${address[$name]}"
done

How do you add flag in $() in bash [duplicate]

This question already has answers here:
"echo -n" prints "-n"
(11 answers)
Closed 3 years ago.
I have the following bash script and seems the -n flag is always interpreted as string in echo
echo Hello, who am I talking to?
read login_name
login_name=$(echo -n $login_name | base64)
echo $login_name | base64 --decode
How do i correct that, or is there any other better syntax for my script?
Better use more portable printf instead of echo -n and use read -p with correct quoting;
read -p 'Hello, who am I talking to? ' login_name
login_name=$(printf '%s' "$login_name" | base64)
printf '%s' "$login_name" | base64 --decode
echo
PS: Just to clarify that there is nothing special about using any flag inside $(...) so one can use it like this:
dt=$(date -u '+%Y/%m/%d')
echo "$dt"

Script: echoing lines from one file to another doesn't print '\t'. Issue [duplicate]

This question already has answers here:
Preserving leading white space while reading>>writing a file line by line in bash
(5 answers)
Closed 6 years ago.
I need to create a file by modifying some lines of a source one.
I developed a loop 'while read line; do'. Inside it, the lines I read and don't modify go just:
echo -e "$line" >> "xxxx.c"
My issue is that some of that lines start with '\t', and they won't print the output file.
Example:
while read line;
do
if echo "$line" | grep -q 'timeval TIMEOUT = {25,0};'
then
echo "$line"
fi
Any help? I've tried with the printf command also but without success.
In that case you could just remove "-e" argument from the echo command.
From echo man page:
-e enable interpretation of backslash escapes

Escaping spaces in path names

I am trying to escape spaces in a path name /Volumes/NO NAME. I need to access files present within this directory via a bash script. I have used sed and printf commands and neither seems to have worked. The code I have so far:
while read line
do
if [[ -d ${line} ]]; then
echo "${line} is a directory. Skipping this.";
elif [[ -f ${line} ]]; then
spacedOutLine=`echo ${line} | sed -e 's/ /\\ /g'`;
#spacedOutLine=$(printf %q "$line");
echo "line = ${line} and spacedOutLine = ${spacedOutLine}";
else
echo "Some other type of file. Unrecognized.";
fi
done < ${2}
Neither of this seems to have worked. For an input like: /Volumes/NO NAME/hello.txt, the output is:
/Volumes/NO: No such file or directory
NAME/hello.txt: No such file or directory
I am on a Mac and using bash via the terminal. I have also gone through a lot of other posts on SO about this which have not helped me.
Use double quotes while calling the ${line} variable.
"${line}"

Command not acting properly in script

The command:
value=${value%?}
will remove the last character from a variable.
Is there any logical reason why it would not work from within a script?
In my script it has no effect whatsoever.
if [[ $line =~ "What I want" ]]
then
if [[ $CURRENT -eq 3 ]]
then
echo "line is " $line
value=`echo "$line" | awk '{print $4}'`
echo "value = "$value
value=${value%?}
echo "value = $value "
break
fi
fi
I cant post the whole script, but this is the piece I refer to. The loop is being entered properly, but the 2 echo $value lines return the same thing.
Edit - this question still stands. The code works fine line bu line in a terminal, but all together in a script it fails.
Echo adds an extra line character to $value in this line:
value=`echo "$line" | awk '{print $4}'`
And afaik that extra char is removed with %?, so it seems it does not change anything at all.
Try echo -n instead, which does not add \n to the string:
value=`echo -n "$line" | awk '{print $4}'`
Since you have provided only the relevant part in the code and not the whole file, I'm going to assume that the first line of your file reads `#!/bin/sh'. This is your problem. What you are trying to do (parameter expansion) is specific to bash, so unless /bin/sh points to bash via a symlink, then you are running the script in a shell which does not understand bash parameter expansion.
To see what /bin/sh really is you can do: ls -l /bin/sh. And to remedy the situation, force the script to run in bash by changing the `shebang' at the top to read `#!/bin/bash'

Resources