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

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

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

How to execute commands read from the txt file using shell? [duplicate]

This question already has answers here:
Run bash commands from txt file
(4 answers)
Closed 4 years ago.
I tried to execute commands read it from txt file. But only 1st command is executing, after that script is terminated. My script file name is shellEx.sh is follows:
echo "pwd" > temp.txt
echo "ls" >> temp.txt
exec < temp.txt
while read line
do
exec $line
done
echo "printed"
if I keep echo in the place of exec, just it prints both pwd and ls. But i want to execute pwd and ls one by one.
o/p am getting is:
$ bash shellEx.sh
/c/Users/Aditya Gudipati/Desktop
But after pwd, ls also need to execute for me.
Anyone can please give better solution for this?
exec in bash is meant in the Unix sense where it means "stop running this program and start running another instead". This is why your script exits.
If you want to execute line as a shell command, you can use:
line="find . | wc -l"
eval "$line"
($line by itself will not allow using pipes, quotes, expansions or other shell syntax)
To execute the entire file including multiline commands, use one of:
source ./myfile # keep variables, allow exiting script
bash myfile # discard variables, limit exit to myfile
A file with one valid command per line is itself a shell script. Just use the . command to execute it in the current shell.
$ echo "pwd" > temp.txt
$ echo "ls" >> temp.txt
$ . temp.txt

Bash Script Variable

#!/bin/bash
RESULT=$(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
for i in $(RESULT);
do
echo "$i"
FILENAME="$(dirname $RESULT)"
done
I have a problem with the line FILENAME="$(dirname $RESULT)". Running the script in debugging mode(bash -x script-name), the ouput is:
test.sh: line 9: RESULT: command not found
For some reason, it can't take the result of the variable RESULT and save the output of dir command to the new variable FILENAME. I can't understand why this happens.
After lots of tries, I found the solution to save full path of finame and finame to two different variables.
Now, I want for each finame, find non-case sensitive of each filename. For example, looking for file image.png, it doesn't matter if the file is image.PNG
I am running the script
while read -r name; do
echo "$name"
FILENAME="$(dirname $name)"
BASENAME="$(basename $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
and then enter the command:
find . $FILENAME -iname $BASENAME
but it says command FILENAME and BASENAME not found.
The syntax:
$(RESULT)
denotes command substitution. Saying so would attempt to run the command RESULT.
In order to substitute the result of the variable RESULT, say:
${RESULT}
instead.
Moreover, if the command returns more than one line of output this approach wouldn't work.
Instead say:
while read -r name; do
echo "$name"
FILENAME="$(dirname $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
The <(command) syntax is referred to as Process Substitution.
for i in $(RESULT) isn't right.You can use $RESULT or ${RESULT}

Shell Script and Command Line Discrepancies

I was wondering if there were specific permissions that were associated with a shell script or if some variable references are taken as being syntactically different.
I tried my short renaming script below:
#!/bin/bash
echo "Starting Renaming Script"
for file in ./*
do
rename=$(echo $file | sed 's/\(img_\)\([0-9]*-[0-9]*\)-\([0-9]*\)_\([0-9]*\).jpg/newyears_20\3-\2_0\4.jpg/')
mv $file $rename
done
All it does is rename a few files, but I noticed that it would work on the command line, but not in the shell script when I ran sh rename.sh
I got the error
rename.sh: syntax error at line 7: `rename=$' unexpected
Is variable assignment handled differently in the shell than on the command line?
Different shells handle commands differently. Your script is a bash script (as identified on the first line #!/bin/bash), therefore it needs to be run by bash, not sh.
bash rename.sh

grep in bash script not working as expected

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?

Resources