Shell script is a directory issue [duplicate] - linux

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 1 year ago.
echo $dir #prints chia-blockchain/
echo $dir | sed -e s,-blockchain/,, # prints chia
forkname = `$dir | sed -e s,-blockchain/,,`# not working
For some reason last line gives me this error :
./newfork.sh: line 15: chia-blockchain/: Is a directory
./newfork.sh: line 15: forkname: command not found

First, doing forkname = ... make the shell believe you are trying to execute a command called forkname. It should be forkname=.... You second problem is that you have $dir | sed..., you have missed out the echo, echo $dir | sed..., in that it is trying to execute whatever is in $dir.

Related

Linux Process substitution Problem: syntax error near unexpected token `<' [duplicate]

This question already has answers here:
Syntax error in shell script with process substitution
(4 answers)
syntax error near unexpected token `<'
(2 answers)
Closed 4 months ago.
See solution at end of post.
I'm running into a frustrating problem. Thanks for any help you can give. What I'm trying to do is get all occurrences of certain values in an error file. I'm using process substitution to parse error output files. I created a test script with the following code and it works perfectly.
#--------------------
# Get all values of affiliate
echo "Building array of affiliates with errors"
while read -r line; do
AffiliateName_array+=($line)
i=$(( ${#AffiliateName_array[#]} - 1 ))
echo " ScriptName_array | index --> $i | $line"
done < <(grep Running ${LogDir}/temp_python_errors.txt | cut -d' ' -f5 | tr '[:lower:]' '[:upper:]')
The echo output looks like this:
Building array of affiliates with errors
index --> 0 | affiliate1
index --> 1 | affiliate2
Then I cut and pasted that exact code into the final script and it throws the error:
path/AR_exit_process.sh: line 166: syntax error near unexpected token `<'
path/Affiliate_Remits/AR_exit_process.sh: line 166: ` done < <(grep Running ${LogDir}/temp_python_errors.txt | cut -d' ' -f5 | tr '[:lower:]' '[:upper:]')'
Here's the code from my final script (exactly the same as the test script since it was cut and pasted):
#--------------------
# Get all values of affiliate
echo "Building array of affiliates with errors"
while read -r line; do
AffiliateName_array+=($line)
i=$(( ${#AffiliateName_array[#]} - 1 ))
echo " ScriptName_array | index --> $i | $line"
done < <(grep Running ${LogDir}/temp_python_errors.txt | cut -d' ' -f5 | tr '[:lower:]' '[:upper:]')
NOTES:
All scripts have the #!/bin/bash bang line
The scripts run on the same server
The test script is called directly from the command line but
the final script is 2 layers deep. All calling scripts use #!/bin/bash
My main reference:
https://wiki.bash-hackers.org/syntax/expansion/proc_subst
SOLUTION: As Charles suggested I added a line to both the test and final scripts with the command: set -o I found that when running the test script, I got the value posix off. However, when I ran the final script with set -o I got posix on. What this means is that the test script was running under bash while the final script was running under sh. The solution is to add the line set +o posix to turn posix off before executing the code. What I'm still not clear about is how the posix setting is different on the same server. More research ...

How to set if condition in bash script? [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
bash, command not found [duplicate]
(2 answers)
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 2 years ago.
so i am new to bash scripting and trying to setup my first conditional block. I am using a shell script to get a JSON response and when the returned item is 0 i want it to do something.
QUEUE_SIZE=$(curl url | jq '.items | length')
if ["$QUEUE_SIZE" -eq "0"]
then
echo "Hey this is cool!!"
fi
When i run the above script, i get the following error: line 3: [0: command not found
Can someone please correct what i'm doing wrong? Thank you for the assistance.
Minor revision:
#!/bin/bash
QUEUE_SIZE=$(curl url | jq '.items | length')
if [ "$QUEUE_SIZE" == "0" ]; then
echo "Hey this is cool!!"
fi

Bash how to store in a variable the result of a linux command? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 2 years ago.
I'm trying to store in a variable the temperature of the computer. I tried this but it doesn't work:
#!/bin/bash
temp = cat "/sys/class/thermal/thermal_zone0/temp"
echo "$temp"
i tried this too:
#!/bin/bash
temp = $(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"
but nothing works, it always says
./temp.sh: line 2: temp: command not found
Spaces are crucial! This works fine:
# NO space around `=`
temp=$(cat "/sys/class/thermal/thermal_zone0/temp")
echo "$temp"

How to reverse a for loop in a bash script [duplicate]

This question already has answers here:
How to loop over files in natural order in Bash?
(7 answers)
Closed 5 years ago.
I need to reverse a for loop in a bash script. There is a directory with videos named after %Y%m.mp4 (201701.mp4; 201702.mp4, 201703.mp4, ...). The loop should start with the oldest filename (e.g. 201712.mp4) How to reverse my for loop?
outputdir="/path/to/video/monthly/"
for file in "$outputdir"*.mp4
do
echo $file
done
You can list your files in a reserved order in the following way:
ls -1 $outputdir/*.mp4 | sort -r
So in you script you can do:
outputdir="/path/to/video/monthly/"
for file in $(ls -1 $outputdir*.mp4 | sort -r)
do
echo $file
done
NOTE: As #PesaThe pointed out this solution would fail to work with filenames with spaces. If it is the case for you, you should quote the command: "$(ls -1 $outputdir*.mp4 | sort -r)" and use "$file"
UPDATE:
See the following test
mkdir test && cd $_
for i in {1..5}; do touch test_$i.txt; done
cd -
And ls -1 test/*.txt will output:
test/test_1.txt
test/test_2.txt
test/test_3.txt
test/test_4.txt
test/test_5.txt
And ls -1 test/*txt | sort -r will output:
test/test_5.txt
test/test_4.txt
test/test_3.txt
test/test_2.txt
test/test_1.txt

Linux bash line override itself [duplicate]

This question already has answers here:
Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?
(3 answers)
Closed 7 years ago.
I made my own .bashrc (part of code below) and in test by pressed arrows up/down: i found that
history of commands override static text of line or leaves last command and print new over it. How to fix that?
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RESTORE=$(echo -en '\033[0m')
RKNAME=$(echo -en '\033[38;5;106m')
RKPATH=$(echo -en '\033[38;5;229m')
RKBRANCH=$(echo -en '\033[38;5;44m')
RKGIT=$(echo -en '\033[38;5;250m')
pathToFolder=$PWD;
if [[ $pathToFolder =~ .*_GitRepo.* ]]
then
PS1='${RKNAME}\u#\h${RESTORE} ${RKGIT}Git ${RKPATH}\W\ ${RKBRANCH}$(parse_git_branch) \n ${RKGIT}-- $ ${RESTORE} '
fi
Resolve my problem is:
However, I had the same line-wrapping problem you did. The fix was to insert [ and ] around the ANSI escapes so that the shell knows not to include them in the line wrapping calculation.
Thanks, #Gillies for link Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?

Resources