How to read hyperlink into variable [duplicate] - linux

This question already has an answer here:
Reading input in bash in an infinite loop and reacting to it
(1 answer)
Closed 6 years ago.
I need to input into variable hyperlink and then download it via wget
Link is:
http://st-im.xyz.com/im/poster/2/1/0/xyz.com-Qwerty-2107544.jpg
But it seems like bash is not writing my link into variable. So, how can I fix this?
Code:
read $link
echo "Link is"
echo $link
wget "$link"
Output:
http://st-im.xyz.com/im/poster/2/1/0/xyz.com-Qwerty-2107544.jpg # This is my input
Link is
http://: Invalid host name.

You are reading the value wrong.
It should be:
read link
$variable is for variable substitution. (Retrieving the value). Not for assigning the value.

Related

Bash script file as input [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
i am trying to give an file as input to me my shell script:
#!/bin/bash
file ="$1"
externalprogram "$file"
echo 'unixcommand file '
i am trying to give the path to my file but it says always
cannot open `=/home/username/documents/file' (No such file or directory)
my path is this /home/username/Documents/file
i do this in terminal : ./myscript.sh /home/username/Documents/file
can someone help me please?
When you say
file ="$1"
with a space after "file", you're running something called file with =$1 as an argument. There probably actually is a utility called file. If you want to assign $1 to a variable called file, you don't need the space:
file="$1"
there shouldn't be a space before = in the second line.
file=$1 should be good enough.
Check what shellcheck says about your code:
^-- SC1068: Don't put spaces around the = in assignments (or
quote to make it literal).
You can read more about SC1068 case on its Github
page.
#!/bin/bash
file=$1
code $file
echo "aberto o arquivo ${file} no vscode"
I made this code snippet to demonstrate, I pass a path and it opens the file in vscode

bash string manipulation got errors in jenkins shell (not pipeline) [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
i am trying to get value of variable by given string:
Running this code on Jenkins and its get "bad substitution"
in regular shell it works.
example:
param1="hello"
param2="world"
PARAMS="param1 param2"
for p in $PARAMS;do
echo ${!p}" "
done
what the best way to make it work in Jenkins too.
You must use with $ sign before the name of the variable:
PARAMS="$param1 $param2"
for p in $PARAMS;do
echo ${p}" "
done

Is there any difference in how variables are referenced in shell script? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Consider the following code:
name=John
echo ${name}
It prints "John", just as expected. Now consider this code:
name=John
echo $name
Again, this code prints "John" just as expected. Both codes work fine.
But I wonder is there any difference between the two, e.g. compatibility?
In your case, there is no difference.
In this case, there is:
name=John
echo ${name}Doe
echo $nameDoe
Read more: here

Shell script won't run properly when re-assigning variable [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Variable variable assignment error -"command not found"
(1 answer)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
So I've got a shell script to do some lazy stuff for if the directory isn't changing for a user. It's below. Essentially, it should be an if statement that if the user enters "default" for the directory, it'll pull them to the default directory for the files. However, I'm getting a command not found on line 16, which is the reassignment statement.
The entire if statement below:
if [ $directory = "default" ];
then
echo Enter your ldap:
read $ldap
$directory = "/usr/local/home/google/${ldap}/Downloads"
fi
I've tried doing it without the dollar sign too...nothing. What's going on here? New to shell, couldn't find this question asked before either.
This is how you should assign a value to a variable in shell:
directory="/usr/local/home/google/${ldap}/Downloads"
No dollar ($) sign.
No space around equal (=) sign.
Also, you should wrap your variables inside double quotes ("). This way, you avoid errors with undefined variables, arguments with spaces, etc.
That gives us:
if [ "$directory" = "default" ]
then
echo "Enter your ldap:"
read $ldap
directory="/usr/local/home/google/${ldap}/Downloads"
fi

Change value of variable insead loop Bash [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
I'm newer on bash scripting ,I have a global variable that I want to change his value insead a loop in my script but still get an error that commande not found
this my script :
SCRIPT_BASE = "/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH = ""
declare -a arr=("A" "B" "C" "D")
for i in "${arr[#]}"
do
if [ $i == "A" ]; then
CURRENT_SCRIPT_PATH = $SCRIPT_BASE
echo -e "Current Path : $CURRENT_SCRIPT_PATH"
fi
done
when I run this script I get that CURRENT_SCRIPT_PATH commande not found
Thanks in advance for any help
In bash you should be really cautious about spaces in if conditions but also when you assign a value to a variable.
Replace in your code the following tree lines:
SCRIPT_BASE="/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH=""
CURRENT_SCRIPT_PATH=$SCRIPT_BASE
If you keep a space after the variable name bash will interpret it as a command and as you do not have commands SCRIPT_BASE, CURRENT_SCRIPT_PATH, CURRENT_SCRIPT_PATH in your current $PATH you have the error command not found that is produced.

Resources