How to get input from command line in a simple bash file? - bash

I think this is the worst error I have ever seen. I have written what I have seen also here:
Example of script but continuously I get error. My plan is to see how read command works. But it seems a dodgy error in my code or terminal. I wondered if anyone has seen similar problem?
#!/usr/bin/bash
echo "Plz enter"
read text
echo "You entered $text"
echo $text
echo "$text"
Error:
$ . test.sh
Plz enter
b
': not a valid identifier
You entered
$

Yes I had the same error in windows with cygwin, I fixed it changing the end of line format from windows format to unix format.
You can also convert the end of line format using Notepad++: Edit > EOL Conversion > UNIX, then save and run the file.

Your input file contains CR+LF line endings.
Remove those and the script should work well. You could use dos2unix or any other utility to remove the CR.

I think, try to change the shell path as like below!
root#sathish:/tmp# vim test.sh
#!/usr/bin/bash
echo "Plz enter"
read text
echo "You entered $text"
echo $text
echo "$text"
root#sathish:/tmp# chmod +x test.sh
root#sathish:/tmp# ./test.sh
-bash: ./test.sh: /usr/bin/bash: bad interpreter: No such file or directory
root#sathish:/tmp# which bash
/bin/bash
root#sathish:/tmp# ls /usr/bin/bash
ls: cannot access /usr/bin/bash: No such file or directory
root#sathish:/tmp# vim test.sh
#!/bin/bash
echo "Plz enter"
read text
echo "You entered $text"
echo $text
echo "$text"
root#sathish:/tmp# ./test.sh
Plz enter
a
You entered a
a
a

Related

Bash Scripting - User Input a Command

Very easy question but I really can't find this when I Google. Sorry!
I'm trying to write a script that runs a user's command that he or she enters but I can't run the command that the user enters.
#!/bin/bash
echo -e "Enter a Command: "
read $COMMAND
echo "Output: $COMMAND" # I can't figure how to implement and print the command
Enter a Command: ls
Output: folder1 folder2 folder3 test.txt)
All you need is to delete the dollar sign from the read command
#!/bin/bash
echo "Enter a Command: "
read COMMAND
echo "Output: $COMMAND"
Happy scripting!, please don't forget to marked as answered ;)
Use eval to execute a command from a variable.
Also, you don't put $ before the variable name in the read command. That's only used when you want to get the variable's value.
#!/bin/bash
echo -e "Enter a Command: "
read COMMAND
echo Output:
eval "$COMMAND"
DEMO
Thanks! This answered my question:
#!/bin/bash
echo -e "Enter a Command: "
read COMMAND
echo Output:
eval "$COMMAND"

Syntax error: unexpected end of file. Bash

I want to set up a teamspeak bot, and I have this script to start this.
#!/bin/bash
if [ $1 = 'stop' ]
then
echo stop >> /root/ts3bot/tmp/log.txt
date >>/root/ts3bot/tmp/log.txt
echo ======================
screen -S bot -X quit
fi
if [ $1 = 'start' ]
then
echo start >> /root/ts3bot/tmp/log.txt
date >> /root/ts3bot/tmp/log.txt
echo ======================
screen -dmS bot php core.php
ps ax | grep -v grep | grep -v -i SCREEN | grep links >> /root/ts3bot/tmp/log.txt
fi
<here is an extra blank line>
but when I type bash bot.sh it says syntax error: unexpected end of file
I don't know what I did wrong :/ the chmod is set on 755
Thanks!
I suspect you may have copied this shell script from a Microsoft Windows box over to a Linux or Unix server. If so, the problem might be that you have DOS/Windows line endings, which can cause unpredictable results in scripts.
To check the script for bad line endings on a Linux or Unix server, you can dump the file (sort of like a hex dump) by typing the following at the shell prompt:
$ od -c bot.sh | less
And look for \n or \r or \r\n. If lines appear to have a \r at the end, then you've found the problem.
To FIX this line-ending problem, you can use a tool like dos2unix if it's installed on your system. If you don't have dos2unix but you're on a Linux server, you may be able to do this instead:
$ sed -i 's/\r//' bot.sh
to convert the file.
Lastly ... see the first line of the script, #!/bin/bash? Because of that, you don't need to run this with bash bot.sh, you can just execute it directly with ./bot.sh.

Redirect to a file which is got from command line

I'm a beginner. :)
I'm trying to ask the name of file from prompt in a shell
and edit that file in another shell like this:
test.sh
echo "enter file name"
read word
sh test2.sh
test2.sh
read number
echo "$number" >> $word
I get an error
Test2.sh: line 1: $mAmbiguous redirect
Any suggestion?
If you want a variable from test.sh to be visible to its child processes, you need to export it. In your case, you would seem to want to export word. Perhaps a better approach would be for test2.sh to accept the destination file as a parameter instead, though.
test.sh
echo "enter file name"
read word
test2.sh "$word"
test2.sh
#!/bin/sh
: ${1?must have destination file name} # fail if missing
read number
echo "$number" >> "$1"

Executing a shell script from a file

My OS platform is this : SunOS machinehull01 5.10 Generic_148888-05 sun4v sparc SUNW,Sun-Fire-T200
I have written a shell script to run from a file
File name: test.sh
#!/bin/sh
VARNAME=$grep '-l' TestWord /home/hull/xml/text/*.txt
echo "Found $VARNAME"
When I run the above command in the console I'm getting the correct output without errors, But when I run sh test.sh or ./test.sh I'm getting below error
test.sh: -l: not found
Found
Can someone please help me on this?
You are searching for so called "command substitution" :
VARNAME=$(grep -l TestWord /home/hull/xml/text/*.txt)
echo "Found $VARNAME"
It will execute the command between $( and the closing parenthesis ) in a subshell and return the output of the command into VARNAME.
Got it.
#!/bin/sh
VARNAME=`grep -l TestWord /home/hull/xml/text/*.txt`
echo "Found $VARNAME"
I had to put those (`)there.

how to log all the command output to one single file in bash scripting [duplicate]

This question already has an answer here:
Output bash script into file (without >> )
(1 answer)
Closed 9 years ago.
In gnu/Linux i want to log all the command output to one particular file.
Say in terminal,i am typing
echo "Hi this is a dude"
It should print in the file name specified earlier without using the redirection in every command.
$ script x1
Script started, file is x1
$ echo "Hi this is a dude"
Hi this is a dude
$ echo "done"
done
$ exit
exit
Script done, file is x1
Then, the contents of file x1 are:
Script started on Thu Jun 13 14:51:29 2013
$ echo "Hi this is a dude"
Hi this is a dude
$ echo "done"
done
$ exit
exit
Script done on Thu Jun 13 14:51:52 2013
You can easily edit out your own commands and start/end lines using basic shell scripting (grep -v, especially if your Unix prompt has a distinctive substring pattern)
Commands launched from the shell inherit the file descriptor to use for standard output from the shell. In your typical interactive shell, standard output is the terminal. You can change that by using the exec command:
exec > output.txt
Following that command, the shell itself will write its standard output to a file called output.txt, and any command it spawns will do likewise, unless otherwise redirected. You can always "restore" output to the terminal using
exec > /dev/tty
Note that your shell prompt and text you type at the prompt continue to be displayed on the screen (since the shell writes both of those to standard error, not standard output).
{ command1 ; command2 ; command3 ; } > outfile.txt
Output redirection can be achieved in bash with >: See this link for more info on bash redirection.
You can run any program with ported output and all its output will go to a file, for example:
$ ls > out
$ cat out
Desktop
Documents
Downloads
eclipse
Firefox_wallpaper.png
...
So, if you want to open a new shell session with ported output, just do so!:
$ bash > outfile
will start a new bash session porting all of stdout to that file.
$ bash &> outfile
will port all of stdout AND stderr to that file (meaning you will no longer see prompts show up in your terminal)
For example:
$ bash > outfile
$ echo "hello"
$ echo "this is an outfile"
$ cd asdsd
bash: cd: asdsd: No such file or directory
$ exit
exit
$ cat outfile
hello
this is an outfile
$
$ bash &> outfile
echo "hi"
echo "this saves everythingggg"
cd asdfasdfasdf
exit
$ cat outfile
hi
this saves everythingggg
bash: line 3: cd: asdfasdfasdf: No such file or directory
$
If you want to see the output and have it written to a file (say for later analysis) then you can use the tee command.
$ echo "hi this is a dude" | tee hello
hi this is a dude
$ ls
hello
$ cat hello
hi this is a dude
tee is a useful command because it allows you to store everything that goes into it as well as displaying it on the screen. Particularly useful for logging the output of scripts.

Resources