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

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.

Related

Is there a way to see the action of a shell script as it is executed? [duplicate]

This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 1 year ago.
I am pretty new here and learning programming.
I wonder if there is a way to see the action of a shell script as it is executed?
For example, I want to see how the values are inserted into the variables in this script.
#!/bin/bash
# Script: potenz2.sh (powers of two with a while loop)
i=0
j=1
while (( i < 12 ))
do
j=$((j * 2))
echo -n "$j"
i=$((i+1))
done
echo ""
Put set -x at the beginning of the script, or run it with that option
bash -x potenz2.sh

“No such file or directory” when comparing numbers in bash [duplicate]

This question already has answers here:
Less than operator '<' in if statement results in 'No such file or directory'
(3 answers)
Closed 6 years ago.
I'm getting an strange error.
#!/bin/bash
echo "Please enter a number"
read var
declare -i num
num=0
while ($num<$var)
do
echo "$num"
done
./loop: line 5: 6: No such file or directory
What am i mistaking?
This is the correct syntax:
while [ "$num" -le "$var" ]
do
echo "$num"
done
What you wrote, $num<$var, is the syntax for running a program with a file as input. Like this:
cat < file.txt
The error is telling you that $var (the content of $var, not literally "var") was not found when Bash attempted to open it as a file.

Problems assigning variable to output of command [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 6 years ago.
I am using wc -l < songs.txt to find out how many lines are in songs.txt
Then I want to store that as a variable. I've tried these:
OUT = $(wc -l < songs.txt)
OUT = "$(wc -l < songs.txt)"
I tried a lot from How to set a variable to the output from a command in Bash?
But I still get command not found error for OUT
wc -l < songs.txt works on its own
What simple thing am I missing here?
Spaces should be avoided.
out=$(wc -l < songs.txt)
it's easy first of all don't use space after and before equal sign
OUT=$(wc -l < songs.txt)
second for call it you can write something like this :
echo $OUT
Good luck

I can't change the value of line_count at the right of pipe , why? [duplicate]

This question already has answers here:
While-loop subshell dilemma in Bash
(4 answers)
Closed 8 years ago.
I occured a problem and I can't find why does it run.
The follow codes is both used to count the line of file 'file.in' , but the first can't change the value of $line_count
The first code is :
#!/bin/bash
line_count=0
cat file.in | while read line; do
let ++line_count
done
echo $line_count
the second code is :
#!/bin/bash
line_count=0
while read line; do
let ++line_count
done < file.in
echo $line_count
Due to use of pipe your first code sample is executing while loop in a sub-shell hence changes made in line_count variable get lost after sub shell exits.

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

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

Resources