This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 6 years ago.
I am writing a bash script which is a follows
#!/bin/bash
#getting the environment variable from commandline
environment=$1
echo $environment
Now when I run the script with bash ./bashScript.sh Hello , I get the following errors on line
: command not found line 2
: command not found line 5
I see that both of these lines are space and bash script is thus giving me an error
To solve it I write my script as
#!/bin/bash
#
#getting the environment variable from commandline
environment=$1
#
echo $environment
But it looks kind of messy
Is there any other way to achieve this. Thanks for help in advance.
Your comment add the precision that your lines use DOS end of lines (\r\n) when you dump it with od -xc file. To avoid it, you should make sure that your editor uses Unix end of lines (\n).
To fix it on an existing text file, you can use tr:
tr -d '\r' < dos_file > unix_file
Related
This question already has answers here:
Bash script - variable content as a command to run
(7 answers)
Closed 1 year ago.
So I have a command that's like
cmd1|cmd2|...|cmdN|execute
The output of cmdN is one line that I want execute to execute as if I copy pasted the output of cmdN into the terminal myself. I've tried to replace execute with bash, with $, and I tried to use xargs. (I'm still kinda confused on each of the options I've tried).
What's the simple answer here that's gonna make me wanna delete this post?
If you need to execute it in the current shell, use the eval command:
eval "$(cmd1|cmd2|...|cmdN)"
If it can be executed in a subshell, pipe to bash:
cmd1|cmd2|...|cmdN | bash
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
i'm coding a simple bash script and i found this error syntax error at line XX `(' unexpected
my code:
function myfun(){
echo XXXX
echo YYYY
read choice
}
choice=$(myfun)
where is the error. i used the ShellCheck and no errors were detected.
Make sure you are running the script with bash. That error is a commonly seen dash shell error.
I suspect the first line of your script is not #!/bin/bash, i.e. you may have left out the shebang line entirely resulting in the default shell being used (which will often be dash especially on Debian derived Linuxes where /bin/sh -> dash).
Try running this:
#!/bin/bash
myfun()
{
echo XXXX
echo YYYY
read choice
}
choice=$(myfun)
This question already has answers here:
Need bash shell script for reading name value pairs from a file
(8 answers)
Reading key/value parameters from a file into a shell script
(1 answer)
How do I grab an INI value within a shell script?
(32 answers)
*export* all variables from key=value file to shell
(1 answer)
Closed 5 years ago.
I am working in linux and have a config file with a lot of single lines formatted like this:
Variable1=Value1
Variable2=Value2
Variable3=Value3
I need something I can run on command line that will echo the value for the respective variable. I have been playing with sed all day, but having a heck of a time. I'm not sure if that's even the best way. Any help would be super.
$ cat a.sh
Variable1=Value1
Variable2=Value2
Variable3=Value3
$ source a.sh
$ echo "$Variable1"
Value1
Note, source will overwrite the value of Variable1 for the current shell.
Search for the variable name and the equal sign, remove them, and print the result.
$ sed -n '/^Variable1=/{s/^Variable1=//;p}' config.txt
Value1
This question already has answers here:
How to echo a variable containing an unescaped dollar sign in bash
(4 answers)
Closed 6 years ago.
I have a bash script, which I run doing the following sudo ./test
The bash script needs to create a repo, and save the following data in it.
So, this is the bash script:
#!/bin/bash
echo "Inputing data... "
echo "[mongodb-org-3.2]
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/" > /etc/yum.repos.d/mongodb-org.repo
However, because the releasever has a $ in front of it, the script save the text like this:
[mongodb-org-3.2]
baseurl=https://repo.mongodb.org/yum/redhat//mongodb-org/3.2/x86_64/
Instead of like this:
[mongodb-org-3.2]
baseurl=https://repo.mongodb.org/yum/redhat/$c/mongodb-org/3.2/x86_64/
Any idea how I can treat the $releasever as text rather than a variable?
I tried putting double quotes around it however that still does not work. I am new to bash scripting so any help appreciated.
Thanks!
Use single quotes to prevent variable expansion.
Using double quotes:
sh-4.1$ baseurl1="https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/"
sh-4.1$ echo $baseurl1
https://repo.mongodb.org/yum/redhat//mongodb-org/3.2/x86_64/
Using single quotes:
sh-4.1$ baseurl2='https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/'
sh-4.1$ echo $baseurl2
https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
Use the back-slash character to indicate that you don't want to call a bash variable, e.g.:
echo \$HELLO
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Capturing multiple line output to a bash variable
For example I want to run ls command and take the return list as a value kept in a array in shell script.
Something like
run
#ls
fileA
fileB
fileC
kept this return list in a variable that keeps a array
variable A = ["fileA","fileB","fileC"];
I cannot give the exact notation for code since I do not know how to write shell script. After I learn this, I 'll.
#!/bin/bash
variableA=$(ls)
echo $variableA
That should be your shell script assuming that you have bash
Then all you'd need to do is chmod +x shell_script to make it executable.
If you use ls > contents.file the result of ls is saved to a file called contents.file.
Remember, > rewrites the entire file while >> appends to the last line.
variableA=$(ls)
echo "$variableA"