grep in bash script not working as expected - linux

If I run
grep -i "echo" *
I get the results I want, but if I try the following simple bash script
#search.sh
grep -i "$1" *
echo "####--DONE--####"
and I run it with sh -x search.sh "echo" I get the following error output:
' grep -i echo '*
: No such file or directory
' echo '####--DONE--####
####--DONE--####
How come? I'm on CentOS

Add the sha-bang line at the top of your script
#!/bin/bash
and after making it executable, run the script using
./search.sh "echo"

The "sh -x" should print the files that '*' matches. It looks like it's not matching any files. Are you maybe running it in a directory with no readable files?

Related

Bash script tee command syntax issue

I want to echo the following line at the end of ~/.profile file using tee command:
export PATH="$HOME/.local/bin:$PATH"
To do this my bash script looks like this
#!/bin/bash
path_env="export PATH="$HOME/.local/bin:$PATH""
echo $path_env| sudo tee -a $HOME/.profile > /dev/null
But whenever I am executing the script it is also executing $PATH and $HOME value and inserts that in ~./profile file which I do not want. I only want the exact line to be passed by the bash script instead of replacing $PATH and $HOME with its own values.
I only want the exact line to be passed by the bash script instead of replacing $PATH and $HOME with its own values.
Och, right, so do not expand it. Quoting.
path_env='export PATH="$HOME/.local/bin:$PATH"'
echo "$path_env" | sudo tee -a "$HOME/.profile" > /dev/null

Errors "No such file or directory" ,"command not found" seen while running my shell script

The contents of my script
`sqlline.py tpxxx.entexxx.org <<END
!outputformat csv
!record /ap_data/DD3/Rawf/Raw_f_$1_$2.csv
select column1,column2,column3,column4 from DD3_vxxv_$1.DD3_vv_RAW_DATA where v_id=$3 and period=$4;
!record
!quit
END;`
`sed -i '1d;$d' /ap_data/DD3/Rawf/Raw_f_$1_$2.csv;
sed -i "s/'//g" /ap_data/DD3/Rawf/Raw_f_$1_$2.csv;
cd /ap_data/D2O/RawDownload/
zip Raw_f_$1_$2.zip Raw_f_$1_$2.csv;
scp Raw_f_$1_$2.zip txxx#daxxxx.entexxx.org:/opt/cdar/common/D2O/TabUpload/;
rm Raw_f_$1_$2.csv Raw_f_$1_$2.zip;`
On executing the script:
./rawfile.sh: line 7: 0/?: No such file or directory
./rawfile.sh: line 13: adding:: command not found
The script gives correct output. But still shows the errors "No such file or directory", "command not found"
My file permission is: -rwxrwxrwx
Your script has backquotes around two main executable sections.
Backquotes cause the content within to be executed in a subshell and substituted back into your script. This means that in your case, your script is executing TWO commands. One of them is the output of the first backquoted expression, and the other is the output of the second backquoted expression.
For example:
$ `echo hello`
bash: hello: command not found
$ `echo echo hello`
hello
What's happening here is that the first echo command generates output which is substituted into your command line, making the command line evaluated by the shell simply "hello" .. which is not a command. The second command line prints "echo hello", which is a valid command line and evaluates to something that prints "hello".
Remove the backquotes from around your two main statements, and just execute the commands directly.
#!/bin/sh
sqlline.py tpxxx.entexxx.org <<END
!outputformat csv
!record /ap_data/DD3/Rawf/Raw_f_$1_$2.csv
select column1,column2,column3,column4 from DD3_vxxv_$1.DD3_vv_RAW_DATA where v_id=$3 and period=$4;
!record
!quit
END
sed -i '1d;$d' /ap_data/DD3/Rawf/Raw_f_$1_$2.csv
sed -i "s/'//g" /ap_data/DD3/Rawf/Raw_f_$1_$2.csv
cd /ap_data/D2O/RawDownload/
zip Raw_f_$1_$2.zip Raw_f_$1_$2.csv
scp Raw_f_$1_$2.zip txxx#daxxxx.entexxx.org:/opt/cdar/common/D2O/TabUpload/
rm Raw_f_$1_$2.csv Raw_f_$1_$2.zip

Crontab not piping to file (LINUX)

The cronjob does not pipe the output from another script to a file but it works I execute it (not same user, chmod for both files is set to 777).
#! /bin/sh
. /disk2/etc/env_cron
SUBJ="Test"
TEXT=/disk2/home/user/mailtxt
ADDR="mail#domain.com"
echo -e `date` > $TEXT
echo -e "1\n\n\nq" | menu >> $TEXT
mutt -s "$SUBJ" -i $TEXT -- $ADDR < /dev/null
I want it to pipe "echo -e 1\n\n\nq" to the script Menu and in turn get the output in a file. The output from Menu will just be text.
The problem (as suggested) was that the cronjob did not have the script 'menu' in it's path. Changing "menu" in the script to the absolute path fixed it.
echo -e "1\n\n\nq" | /folder/folder/menu >> $TEXT
EDIT: Do not forget to set the correct permissions on the textfile if the cronjob is run by another user.

Perl's %ENV doesn't works in one-liner

I write simple bash line that should replace the LOGIN word in some bash script (will replace the word LOGIN to admin word)
But it doesn’t work?
But when I type bash command on my Linux/solaris machine and then run separately the commands then its work
so why the bash one liner not work ( what’s the diff here ? )
bash one liner line
/tmp ROOT > bash -c 'export LOGIN=admin ; /usr/local/bin/perl -i -pe 's/LOGIN/$ENV{LOGIN}/' /tmp/pass_login.bash'
ENV: Undefined variable.
run command separately under bash shell ( works fine )
/tmp ROOT > bash
bash-3.2# export LOGIN=admin
bash-3.2# /usr/local/bin/perl -i -pe 's/LOGIN/$ENV{LOGIN}/' /tmp/pass_login.bash
.
my script
more pass_login.bash
#!/bin/bash
MY_LOG_NAME=LOGIN
Doesn't look to me like you have your quotes/variables escaped properly. Try this instead:
bash -c 'export LOGIN=admin ; /usr/local/bin/perl -i -pe "s/LOGIN/\$ENV{LOGIN}/" /tmp/pass_login.bash'

Linux bash script: share variable among terminal windows

If I do this:
#!/bin/bash
gnome-terminal --window-with-profile=KGDB -x bash -c 'VAR1=$(tty);
echo $VAR1; bash'
echo $VAR1
How can I get the last line from this script to work? I.e., be able to access the value of $VAR1 (stored on the new terminal window) from the original one? Currently, while the first echo is working, the last one only outputs an empty line.
The short version is that you can't share the variable. There's no shared channel for that.
You can write it to a file/pipe/etc. and then read from it though.
Something like the following should do what you want:
#!/bin/bash
if _file=$(mktemp -q); then
gnome-terminal --window-with-profile=KGDB -x bash -c 'VAR1=$(tty); echo "$VAR1"; declare -p VAR1 > '\'"$_file"\''; bash'
cat "$_file"
. "$_file"
echo "$VAR1"
fi

Resources