How to make two echo strings in the same line?
#!/bin/bash
echo "A"
echo "B" | grep B
result is:
A
B
but I want it to be:
AB
You could avoid the line break in the first echo call with echo -n
Related
I wrote a simple bash script and in the end of that I tried to test positional arguments like $0, $1, ...
echo please enter your name
read name
if [ -z "$name" ]
then
echo please enter your name
fi
if [ -n "$name" ]
then
echo Thank you so much
fi
echo $0
echo $1
echo $2
echo $3
After I run that, the output was:
please enter your name
j
Thank you so much
/bin/reza.sh
Why just $0 had output and other had nothing?
Run it like below
./bin/reza.sh first second third
please enter your name
monk
Thank you so much
/bin/reza.sh
first
second
third
Also, $0 is file name of the script itself.
The arguments you enter to a script are take in the order $1,$2,$3 and so on.
In this testscript:
#!/bin/bash
echo $0 #Gives you the command/script name itself
echo $1 #Gives you the first argument
echo $2 #Gives you the second argument
echo $3 #Gives you the third argument
echo $# #Gives you all arguments
echo $# #Gives you the total number of arguments excluding the script name
So the result of
$./testscript a b c
is
./testscript
a
b
c
a b c
3
If the argument is not assigned, its value is null or nothing will be printed.
$ printf "%sThere is nothing before this.\n" $1
gives you :
There is nothing before this.
Note: Don't use echo to test arguments, echo will append a newline at the end automatically as in bash.
I want to merge all files into one. Here, the last argument is the destination file name.
I want to take last argument and then in loop stop before last arguments.
Here code is given that I want to implement:
echo "No. of Argument : $#"
for i in $* - 1
do
echo $i
cat $i >> last argument(file)
done
How to achieve that?
Using bash:
fname=${!#}
for a in "${#:1:$# - 1}"
do
echo "$a"
cat "$a" >>"$fname"
done
In bash, the last argument to a script is ${!#}. So, that is where we get the file name.
bash also allows selecting elements from an array. To start with a simple example, observe:
$ set -- a b c d e f
$ echo "${#}"
a b c d e f
$ echo "${#:2:4}"
b c d e
In our case, we want to select elements from the first to the second to last. The first is number 1. The last is number $#. We want to select all but the last. WE thus want $# - 1 elements of the array. Therefore, to select the arguments from the first to the second to last, we use:
${#:1:$# - 1}
A POSIX-compliant method:
eval last_arg=\$$#
while [ $# -ne 1 ]; do
echo "$1"
cat "$1" >> "$last_arg"
shift
done
Here, eval is safe, because you are only expanding a read-only parameter in the string that eval will execute. If you don't want to unset the positional parameters via shift, you can iterate over them, using a counter to break out of the loop early.
eval last_arg=\$$#
i=1
for arg in "$#"; do
echo "$arg"
cat "$arg" >> "$last_arg"
i=$((i+1))
if [ "$i" = "$#" ]; then
break
fi
done
My variable has value "test one two", i want them to be separated by " " and store in different variable.
#/usr/bin/ksh
var="test one two"
___________________________ command
temp1 = test
temp2 = one
temp3 = two
Assuming you can use bash since you tagged your question with bash, this is probably what you really should do:
$ var="test one two"
$ temp=( $var )
$ echo "${temp[0]}"
test
$ echo "${temp[1]}"
one
$ echo "${temp[2]}"
two
but I suspect whatever script you're using this in is probably employing the wrong approach to solve whatever your larger problem is.
In ksh you can use read:
var="test one two"
echo "$var" | read temp1 temp2 temp3
$ echo "$temp1"
test
$ echo "$temp2"
one
$ echo "$temp3"
two
I have an interactive script that I want to print some lines, overwrite them with blank lines and write some more:
for host in "${!HOSTS[#]}"
do
echo "Running on ${HOSTS[$host]}:"
ssh ${HOSTS[host]} "echo blah ; echo blah"
echo -e "\e[4A"
printf " %$((COLUMNS-1)).s\n" {1..3}
echo -e "\e[4A"
done
This is based that I will actually have 3 lines of previous output.
Is there a way to make it more dynamic? Finding out how many lines were printed and overwriting exactly those?
Thanks
Put the output in a variable, and count the number of lines in the variable:
OUTPUT=$(ssh ${HOSTS[host]} "echo blah ; echo blah")
lines=$(echo "$OUTPUT" | wc -l) # count lines
echo "$OUTPUT" # echo it
After this you can use $lines to know how many lines to overwrite.
I have a bash script that is executing a program in a loop. I want to evaluate each line from the stdout and do something if it matches my condition.
I still want to be able to see stdout on the screen. Is there a simple way to accomplish this? Thanks!
There are several variants of looping over input, but one possibility is thus:
my_cmd | while read line; do
echo "$line"
my_process "$line"
done
This should do what you want:
for string in "a" "b" "c"
do
output=`echo ${string}`
echo ${output}
if [ ${output} == "b" ] ; then
echo "do something"
fi
done
Just replace the first echo with your program.