Unix shell scripting to reverse a string without rev [duplicate] - bash

This question already has answers here:
reverse the order of characters in a string
(14 answers)
Closed 9 years ago.
I want to write a shell script that accepts a string as a command line argument and prints out what is entered in the reverse order with out using the rev command but rather reversing the letters one by one. how would I do that?
so like
if Flower is entered it will print out rewolF
Thanks

A good subject for bash parameter expansion :
#!/bin/bash
for ((i=${#1}; i>=0; i--)); do printf "${1:$i:1}"; done; echo
Example :
./script.sh foobar
Output :
raboof

s1='Flower'
a1=($(echo $s1|fold -w1 ))
for (( i=${#a1[#]}-1;i>=0;i--));do echo "$i=>${a1[i]}"; done
5=>r
4=>e
3=>w
2=>o
1=>l
0=>F

Related

using command line arguments in loop shell [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 3 years ago.
I am trying to use command line arguments for arithmetic but cant seem to find any documentation explaining how to do this. As an example if I use:
for i in {$1..$2}
do
echo $i
done
and call
test.sh 1 20
the following output is produced:
{1..20}
instead of
1
2
3
..
20
The following will also work:
declare -a ary='({'$1..$2'})'
for i in "${ary[#]}"; do
echo "$i"
done
Note that declare is as harmful as eval.
You need to check and sanitize the arguments before use.
There's no way to do this properly without the evil eval() with brace expansion in bash.
You can use seq instead :
for i in $(seq $1 $2); do

Bash script: Comparing number of lines with integer argument [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
im new to bash scripting, appreciate if you can help.
Im trying to write a script to compare the lines in file with integer argument.
Here is what i've got so far but i make some mistakes and get error.
#!/bin/bash
a="$1"
b="wc -l < /filepath/filename.txt"
if (( $a < $b )); then
echo "file has more lines than integer"
else
echo "file has less lines than integer"
fi
Appreciate if you can point to where i make mistake.
b="wc -l < /filepath/filename.txt"
should instead be:
b=$(wc -l < /filepath/filename.txt)
...if you want to run that command and store its output in the variable.

Bash "while read loop" does not properly recognize variables [duplicate]

This question already has answers here:
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
Closed 5 years ago.
I am using MacBook pro terminal to execute a shell script. It loops through a text file and create filenames based on each line in the file.
#!/bin/bash
year=2010
list=list_test.txt
mydir=thisdir
i=1 # counter
while read line
do
echo $i $line
file1=`echo $mydir/file_$year_$line_test.tif`
file2=`echo $mydir/file_$year_$line_test.tif`
echo $file1 $file2
i=$(($i+1))
done < $list
However, the output is peculiar:
1 17019
thisdir/file_.tif thisdir/file_.tif
2 17029
thisdir/file_.tif thisdir/file_.tif
3 17039
thisdir/file_.tif thisdir/file_.tif
Within the loop, bash does not recognize some variables, like "year" which is a global, and "line" which is read from the text file. The text file is as below:
17019
17029
17039
Another script with exactly the same manner works very well. This is mysterious to me now.
Any help or comments are extremely appreciated! Thanks very much!
_ is a valid character for an identifier, but you want to use it as a literal character in the file name. You need to use the full form of parameter expansion, ${x} instead of $x.
(Also, the command substitution isn't necessary.)
file1=$mydir/file_${year}_${line}_test.tif
file2=$mydir/file_${year}_${line}_test.tif

Linux bash - extract value from grep output [duplicate]

This question already has answers here:
Get string after character [duplicate]
(5 answers)
Closed 6 years ago.
I have variable $OUTPUT=abc PHYSIN=lalala ghi
How can I extract the value of PHYSIN into another new variable called VETH_NAME,
in other words, I'd like VETH_NAME to be lalala
How can I do so using bash commands?
Thanks
Assuming this is what you are saying :
OUTPUT="abc"
PHYSIN="lalala ghi"
VETH_NAME=$(echo "$PHYSIN" | cut -d" " -f1)
finally :
echo $VETH_NAME
lalala
Use parameter expansion with the %% operator, which drops the longest matching suffix from the expansion.
OUTPUT=abc
PHYSIN="lalala ghi"
VETH_NAME=${PHYSIN%% *}

How assign rev command to a variable in shell [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I wanted to direct the output of rev command to a variable, I tried different methods and didn't work.
read -p "Enter the number: " n
echo $n | rev
echo "new n is: $p"
I want to assign the output of line 2 to p. How?
Thank you,
To store the output of a command in a variable use a $(...) command substitution:
p=$(echo $n | rev)
For further reference, you can check this link

Resources