Issue in getting count in variable in shell script - shell

I am facing issue with below command. When i am ruuning it is shell it is giving output.
count=`head -92 czh_script_178.log | tail -1 | sed 's/ //'`
But whenb I used it in shell script then it is failing with exit 0;
Can anyone please help in this.
I am using SunOS.

It looks like you want line 92 without some indentation. Here's how to do that:
count="$(sed -n '92s/^[[:space:]]*//p' /absolute/path/to/czh_script_178.log)"
If the path is not absolute in the script, it will fail unless $PWD is the same as the directory of the log file.

Related

Cygwin Command Substitution not Working

I am trying to trouble shoot a problem I am seeing when running bash commands in Cygwin.
I am trying to assign the CLang version from a text file to a variable. If I run this in Cygwin:
$ (sed -n 1p "$CLANGC2_VERSION_FILE" | sed 's/\s//g')
I get this output (which is exactly what I want):
14.10.25903
Now, if I try and assign this to a variable it doesn't work. Here is what I am trying:
$ CLANGC2_VERSION=$(sed -n 1p "$CLANGC2_VERSION_FILE" | sed 's/\s//g')
but when I inspect or print the variable, it is empty.
What am I doing wrong?
Turns out that there is a known 'Big List of Dodgy Apps' (BLODA) which can interfere with Cygwin and bash.
The discussion I found is here: https://cygwin.com/ml/cygwin/2017-07/msg00197.html
The BLODA list is here: https://cygwin.com/faq/faq.html#faq.using.bloda
Turns out my AntiVirus is on the list.
I've removed the AV and now the commands work. There must be some low-level stuff going with the AV that causes it to fail.
You can use backticks to get the desired results.
CLANGC2_VERSION=`(sed -n 1p "$CLANGC2_VERSION_FILE" | sed 's/\s//g')`

Unable to modify file as part of entry point command

My Dockerfile's entry point CMD executes a shell script to modify a local file based on an environment variable before executing my application (Flask). The shell script is like so:
cat static/login.html | sed "s/some_match/some_substitute/g" > static/login.html
However, I am finding that the resulting file is zero bytes. Any ideas what might be going on?
Thanks.
I can reproduce your problem, but not explain it. Maybe other answers follow.
But here is a solution which fixes the problem:
Use a different file name for the output than for the input.
cat input.txt | sed "s/a/b/g" > input2.txt
Alternatively, use the -i.bak option.
sed -i.bak "s/b/a/g" input.txt
I can only speculate about what exactly is going on:
Maybe the output of your pipe is opened for writing (non-appending) before the input is read.

Bash. Redirecting output of grep into file

I have a basic bash script that i want to use to create a list of directories inside a file. The issue is that when i type the following command on the terminal it works fine but when used inside a function it does not
function Create_file_dirs()
{
ls | grep / > dirs.txt
cat dirs.txt #used for debugging
}
This function is inside a script and i'm running it by calling the script. ./script.sh
`
The command
ls | grep / > dirs.txt
works great on the terminal window and produces a file listing directories line by line, but doesn't do anything when used inside a function and i can't figure out why. Thanks
ls without options does not produce slashes, so grep doesn't find anything. You can easily reproduce it on the command line by, say,
comand ls|grep /
My thanks to William Pursell for his answer and to everyone who contributed.
The answer came from a comment made by William Pursell.
The working function is:
create_file_dirs()
{
ls -F | grep / >./dirs.txt
cat dirs.txt
}
The issue was,that when i was running the ls command on the terminal i could see a slash / after a directories name and running the command on the terminal actually produced the desired result.
ls | grep / > ./dirs.txt
It seems though running ls through a script doesn't produce a slash / after a directories name that is why i had to add them -F option to resolve this issue.
The way i tested it out was with this simple function inside a script and run the script. The result didn't have slashes.
test_dirs(){
ls | cat
}

Unix grep is failing in Ruby

I am trying to use Unix grep in my ruby code which I am able to run my shell terminal but it is not working when I am calling it through my ruby code. Can someone help me finding the problem?
I am trying to read the lines that match the pattern that starts with /platform/app_name and ends with username or service_name or hostname or port in config.txt file and write them into sub_config.txt file. Below is the piece of code that is blocking me right now.
exec ("cd #{$USER_HOME}; grep -E \'(^/platform/app_name/.*/username) | (^/platform/app_name/.*/port) | (^/platform/app_name/.*/service_name) | (^/platform/app_name/.*/hostname) \' config.txt > .sub_config.txt")

Bash statement meaning

I'm working on a project, and it's being run by an autoscript. The script has the following line:
./executable ./dev | grep -i "GET.*index.*200" > ./dev/logs/log1
I have my code writing to stdout, but it never gets written to log1. If I change it though and remove the grep command, it writes just fine. Any help would be appreciated, as I seemingly don't understand grep as well as I should.
You might try to redirect std output in your script "executable" using commands:
exec > ./dev/logs/log1
exec 2> ./dev/logs/errlog1
So, now not need to use ">" in the line
./executable ./dev | grep -i "GET.*index.*200"
Also I recommend you to use only absolute paths in scripts.
ps. [offtop] I can't write comments yet (not enough reputation).

Resources