Hi I am trying to build a program in bash. The idea is that I would have open two terminals. One would take the input with a pipe cat > pipe . The other terminal would be running a bash script with a while true loop and read the input from the pipe. The input would be stored to a variable and further action would occur depending on what is stored inside. This is what I tried.
The program gets the pipe name as an argument and it is stored to the variable pipe.
while true; do
input=$(cat<$pipe)
if [ "$input" == "exit" ]; then
exit 0
fi
done
I have tried to input an exit string throught the pipe but the program does not stop as it should. If the variable does not get any value from the pipe how would I correct that? Or is something else wrong that prevents the exit from happening?
Your second script should be something like :
#!/bin/bash
pipe="$1" # Here $1 is full path to the file pipe as you have confirmed
while true
do
input=$(cat<"$pipe")
if [[ $input =~ exit ]] #original line was if [ "$input" == "exit" ]
then
exit 0
fi
done
Remember $(cat<"$pipe") will store the entire file as a string. So, even though you have the word exit somewhere down in the pipe file the original condition if [ "$input" == "exit" ] will be false except the case when the first word you enter for cat>pipe itself is "exit".
A little tweakish solution is do a regex match (=~) as I have done but this solution is not very reliable because if you enter something like "I don't wanna exit", for cat>pipe the second script will exit.
A second solution would be :
#!/bin/bash
pipe="$1"
while true; do
input=$(tail -n1 "$pipe") #checking only the last line
if [[ "$input" == "exit" ]] #original if condition
then
exit 0
fi
done
Related
#!/bin/bash
#if there are no args supplied exit with 1
if [ "$#" -eq 0 ]; then
echo "Unfortunately you have not passed any parameter"
exit 1
fi
#loop over each argument
for arg in "$#"
do
if [ -f arg ]; then
echo "$arg is a file."
#iterates over the files stated in arguments and reads them $
cat $arg | while read line;
do
#should access only first line of the file
if [ head -n 1 "$arg" ]; then
process line
echo "Script has ran successfully!"
exit 0
#should access only last line of the file
elif [ tail -n 1 "$arg" ]; then
process line
echo "Script has ran successfully!"
exit 0
#if it accesses any other line of the file
else
echo "We only process the first and the last line of the file."
fi
done
else
exit 2
fi
done
#function to process the passed string and decode it in base64
process() {
string_to_decode = "$1"
echo "$string_to_decode = " | base64 --decode
}
Basically what I want this script to do is to loop over the arguments passed to the script and then if it's a file then call the function that decodes in base64 but just on the first and the last line of the chosen file. Unfortunately when I run it even with calling a right file it does nothing. I think it might be encountering problems with the if [ head -n 1 "$arg" ]; then part of the code. Any ideas?
EDIT: So I understood that I am actually just extracting first line over and over again without really comparing it to anything. So I tried changing the if conditional of the code to this:
first_line = $(head -n 1 "$arg")
last_line = $(tail -n 1 "$arg")
if [ first_line == line ]; then
process line
echo "Script has ran successfully!"
exit 0
#should access only last line of the file
elif [ last_line == line ]; then
process line
echo "Script has ran successfully!"
exit 0
My goal is to iterate through files for example one is looking like this:
MTAxLmdvdi51awo=
MTBkb3duaW5nc3RyZWV0Lmdvdi51awo=
MXZhbGUuZ292LnVrCg==
And to decode the first and the last line of each file.
To decode the first and last line of each file given to your script, use this:
#! /bin/bash
for file in "$#"; do
[ -f "$file" ] || exit 2
head -n1 "$file" | base64 --decode
tail -n2 "$file" | base64 --decode
done
Yea, as the others already said the true goal of the script isn't really clear. That said, i imagine every variation of what you may have wanted to do would be covered by something like:
#!/bin/bash
process() {
encoded="$1";
decoded="$( echo "${encoded}" | base64 --decode )";
echo " Value ${encoded} was decoded into ${decoded}";
}
(( $# )) || {
echo "Unfortunately you have not passed any parameter";
exit 1;
};
while (( $# )) ; do
arg="$1"; shift;
if [[ -f "${arg}" ]] ; then
echo "${arg} is a file.";
else
exit 2;
fi;
content_of_first_line="$( head -n 1 "${arg}" )";
echo "Content of first line: ${content_of_first_line}";
process "${content_of_first_line}";
content_of_last_line="$( tail -n 1 "${arg}" )";
echo "Content of last line: ${content_of_last_line}";
process "${content_of_last_line}";
line=""; linenumber=0;
while IFS="" read -r line; do
(( linenumber++ ));
echo "Iterating over all lines. Line ${linenumber}: ${line}";
process "${line}";
done < "${arg}";
done;
some additions you may find useful:
If the script is invoked with multiple filenames, lets say 4 different filenames, and the second file does not exist (but the others do),
do you really want the script to: process the first file, then notice that the second file doesnt exist, and exit at that point ? without processing the (potentially valid) third and fourth file ?
replacing the line:
exit 2;
with
continue;
would make it skip any invalid filenames, and still process valid ones that come after.
Also, within your process function, directly after the line:
decoded="$( echo "${encoded}" | base64 --decode )";
you could check if the decoding was successful before echoing whatever the resulting garbage may be if the line wasnt valid base64.
if [[ "$?" -eq 0 ]] ; then
echo " Value ${encoded} was decoded into ${decoded}";
else
echo " Garbage.";
fi;
--
To answer your followup question about the IFS/read-construct, it is a mixture of a few components:
read -r line
reads a single line from the input (-r tells it not to do any funky backslash escaping magic).
while ... ; do ... done ;
This while loop surrounds the read statement, so that we keep repeating the process of reading one line, until we run out.
< "${arg}";
This feeds the content of filename $arg into the entire block of code as input (so this becomes the source that the read statement reads from)
IFS=""
This tells the read statement to use an empty value instead of the real build-in IFS value (the internal field separator). Its generally a good idea to do this for every read statement, unless you have a usecase that requires splitting the line into multiple fields.
If instead of
IFS="" read -r line
you were to use
IFS=":" read -r username _ uid gid _ homedir shell
and read from /etc/passwd which has lines such as:
root:x:0:0:root:/root:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
then that IFS value would allow it to load those values into the right variables (in other words, it would split on ":")
The default value for IFS is inherited from your shell, and it usually contains the space and the TAB character and maybe some other stuff. When you only read into one single variable ($line, in your case). IFS isn't applied but when you ever change a read statement and add another variable, word splitting starts taking effect and the lack of a local IFS= value will make the exact same script behave very different in different situations. As such it tends to be a good habbit to control it at all times.
The same goes for quoting your variables like "$arg" or "${arg}" , instead of $arg . It doesn't matter when ARG="hello"; but once the value starts containing spaces suddenly all sorts of things can act different; suprises are never a good thing.
i have to keep the yes and no choice but i want to put something that if i don't type yes or no to show that this is not possible i am new to this and not sure what to do
echo "Do you wish to Exit?(yes/no)"
read input
if [ "$input" == "yes" ]
then
clear
exit
elif [ "$input" == "no" ]
then
clear
echo "Reloaded"
echo -e "\n"
elif [ "$input" == * ]
then
echo "Invalid Input"
fi ;;
You are on the right track here, the last condition check is causing issues however.
You are checking if "$input" == * in the last block. When you use * on its own like that you can get some wacky behavior. The shell tries to expand it to all the files in the current directory. This means that you will likely be giving too many arguments to the conditional and should get an error similar to [: too many argument when there are several files in the current directory. If the directory is empty except for the given script the conditional will be expanded to elif [ "$input" == some_file.txt] and the script will continue and exit normally without the desired output. See the bash pattern matching and command expansion documentation.
The simplest solution here is to use an else instead. This block will execute if the first two conditions are not met, and therfore $inputs is something other than yes or no. See the bash conditional documentation. You script should look something like this:
echo "Do you wish to Exit?(yes/no)"
read input
if [ "$input" == "yes" ]
then
clear
exit
elif [ "$input" == "no" ]
then
clear
echo "Reloaded"
echo -e "\n"
else
echo "Invalid Input"
fi
As a final comment, you can simplify the read command into 1 line by leveraging the -p argument, from the usage:
-p prompt output the string PROMPT without a trailing newline before attempting to read
So you can condense your read into read -p 'Do you wish to Exit? (yes/no)' input
Below is the code of bash:
a=`echo hello`
echo $a
output is :
hello
But I think it should be:
hello
0
You think wrong ;-)
Putting the command in backticks assigns the output (stdout) from the expression on the right to the variable on the left.
$? gives you the "output status" (or return code) of the command - aka the "0" you were expecting.
So:
a=`echo hello`
Runs the command "echo hello" but instead of echoing to stdout, it "echoes" to varaiable a. So a=whatever_the_command_would_have_written_to_stdout (in this case "hello") - nothing is actually written to stdout because it is "captured" by the ``s
You mistakenly think that a=`echo hello`:
executes echo hello and prints its stdout output directly to the caller's stdout,
and then assigns the exit code (return value) of the echo command to variable $a.
Neither is true; instead:
echo hello's stdout output is captured in memory (without printing to the caller's stdout; that's how command substitutions work),
and that captured output is assigned to $a.
A command's exit code (a return value indicating success vs. failure) is never directly returned in POSIX-like shells such as Bash.
The only way to use an exit code is either:
explicitly, by accessing special variable $? immediately after the command ($? contains the most recent command's exit code)
implicitly, in conditionals (a command whose exit code is 0 evaluates to true in a conditional, any other exit code implies false).
Thus, to achieve what you're really trying to do, use:
echo 'hello' # Execute a command directly (its stdout output goes to the caller's stdout)
a=$? # Save the previous command's exit code in var. $a
echo "$a" # Echo the saved exit code.
As this [ this ] answer already mentioned, the return value for the last executed command is stored in
$? # ${?} is sometimes needed
If you wish a to contain 'hello' and the return value of echo hello in separate lines, ie
hello
0
below is one way to do it
$ a=`echo -en "hello\n" && echo -n $?` # here $? is ret val for 1st echo
$ echo -e "$a"
hello
0
Note
-n with echo suppresses the trailing new line
-e with echo interprets escape sequences
Since && is the logical and operator, the second echo wouldn't have been executed had the first echo failed
Another important point to note is that even the assignment ie
a=b
has a return value.
So I have this bash script
function exec_do(){
while [[ 1 ]]; do
read _INPUT
if [$_INPUT -eq "exit"]
then
break
else
echo $_INPUT
fi
done
}
The intention is so that if I type in exec_do then it does a while loop which reads the input and do stuff based on the input.
If the input is exit, then it exits the while loop
If the input is not exit then it echoes it
However when I run exec_do
and then type in input
It instead returns input: command not found
And moreover typing in "exit" doesn't break the loop and also return command not found
What did I do wrong and how can I fix this?
Your comparison for _$INPUT is a string, then you need ==. If comparing integers, you need -eq. Also double quote the $_INPUT variable for strings if they might contain white spaces or meta characters.
#!/bin/bash
function exec_do(){
while [[ 1 ]]; do
read _INPUT
if[ "$_INPUT" == "exit" ]; then
break
else
echo $_INPUT
fi
done
}
exec_do
I'm not used to writing Bash scripts, and Google didn't help in figuring out what is wrong with this script:
#!/bin/bash
while read myline
do
done
echo "Hello"
while read line
do
done
exit 0
The output I get is:
./basic.agi: line 4: syntax error near unexpected token 'done'
./basic.agi: line 4: 'done'
and my bash version is:
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Thank you.
Edit: The script works OK when the while loop isn't empty.
While I'm at it... I expected to exit the loop when the user typed nothing, ie. simply hit the Enter key, but Bash keeps looping. How can I exit the loop?
while read myline
do
echo ${myline}
done
echo "Hello"
while read line
do
true
done
exit 0
You can't have an empty loop. If you want a placeholder, just use true.
#!/bin/bash
while read myline
do
true
done
or, more likely, do something useful with the input:
#!/bin/bash
while read myline
do
echo "You entered [$line]"
done
As for your second question, on how to exit the loop when the user just presses ENTER with nothing else, you can do something:
#!/bin/bash
read line
while [[ "$line" != "" ]] ; do
echo "You entered [$line]"
read line
done
When you are using the read command in a while loop it need input:
echo "Hello" | while read line ; do echo $line ; done
or using several lines:
echo "Hello" | while read line
do
echo $line
done
This is a way to emulate a do while loop in Bash, It always executes once and does the test at the end.
while
read -r line
[[ $line ]]
do
:
done
When an empty line is entered, the loop exits. The variable will be empty at that point, but you could set another variable to its value to preserve it, if needed.
while
save=$line
read -r line
[[ $line ]]
do
:
done
What are you trying to do? From the look of it it seems that you are trying to read into a variable?
This is done by simply stating read the value can then be found inside of $
ex:
read myvar
echo $myvar
As other have stated the trouble with the loop is that it is empty which is not allowed.
If you are looking to create a menu with choices as a infinite loop (with the choice to break out)
PS3='Please enter your choice: '
options=("hello" "date" "quit")
select opt in "${options[#]}"
do
case $opt in
"hello") echo "world";;
"date") echo $(date);;
"quit")
break;;
*) echo "invalid option";;
esac
done
PS3 is described here
If you type help while in bash you'll get an explanation of the command.