how to use `amp;` & `gt;` commands in linux-shell? - linux

I downloaded a Bash script from Internet.
However, when I try to run it, it fails because of the && commands and prints the following error message:
No command 'amp' found
What is amp ?
What is gt ?
What do they do?

I suspect this has undergone an HTML entity translation. You want to reverse this e.g.
& becomes &
and
> becomes >
So (for example)
cd /dir && ls > filename
would become
cd /dir && ls > filename

in other word, & is the character reference for "An ampersand". While gt is simply we call it as (greater than).

&amp and &gt are html escape codes for & and > respectively.
Either you have downloaded a script that has been htmlized, or you've accidentally downloaded a webpage with the script on.
Check the top line of the file. if it starts with something like #!/usr/bin/bash then it's the former and you just need to reverse the changes. If it has something like a <html> tag as the start, then you've downloaded the webpage - go back to where you got it and look for something like a 'raw' link

Related

sh shell redirecting a subshell to file, can't find the right syntax

i want to run a sed command with programatically with changing parameters.
the thing is that i cant find the correct syntax to do so.
i want to configure a conf file with this and
change a dir path to another.
i'm currently using:
RESULT=$("sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf")
and i get the error message:
./change_tst.sh: line 7: sed 's/--ROOT_DIR--//home/test_dir/g' /home/tst/conf.conf > /home/script_tst/conf.conf: No such file or directory
the ">" is not working for some reason.
what am i doing wrong? or what is the best way to do this ?
UPDATE
i drooped the result variable and now running this:
(sed 's/--ROOT_DIR--/$root_inst_dir/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf) > ${SOURCE_DIR}/${tool_name}.conf
the new file is being created in > ${SOURCE_DIR}/${tool_name}.conf,
but the search/replace is happening literally and not as a variable...
thanks.
Putting " inside parenthesis will result in bash wanting to execute a command named exactly:
sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf"
Such command does not exist on your system.
Probably you intended to put " outside $(...):
RESULT="$(sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf)"
Better way, if you don't need the RESULT variable and if you want to properly escape root_inst_dir variable:
sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"
Or if you need RESULT variable:
sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"
RESULT=$(cat ${SOURCE_DIR}/${tool_name}.conf)

Obtaining file names from directory in Bash

I am trying to create a zsh script to test my project. The teacher supplied us with some input files and expected output files. I need to diff the output files from myExecutable with the expected output files.
Question: Does $iF contain a string in the following code or some kind of bash reference to the file?
#!/bin/bash
inputFiles=~/project/tests/input/*
outputFiles=~/project/tests/output
for iF in $inputFiles
do
./myExecutable $iF > $outputFiles/$iF.out
done
Note:
Any tips in fulfilling my objectives would be nice. I am new to shell scripting and I am using the following websites to quickly write the script (since I have to focus on the project development and not wasting time on extra stuff):
Grammar for bash language
Begginer guide for bash
As your code is, $iF contains full path of file as a string.
N.B: Don't use for iF in $inputFiles
use for iF in ~/project/tests/input/* instead. Otherwise your code will fail if path contains spaces or newlines.
If you need to diff the files you can do another for loop on your output files. Grab just the file name with the basename command and then put that all together in a diff and output to a ".diff" file using the ">" operator to redirect standard out.
Then diff each one with the expected file, something like:
expectedOutput=~/<some path here>
diffFiles=~/<some path>
for oF in ~/project/tests/output/* ; do
file=`basename ${oF}`
diff $oF "${expectedOutput}/${file}" > "${diffFiles}/${file}.diff"
done

Renaming files like 20141207_190822.jpg to "2014-12-07 19.08.22.jpg" in linux or MacOS X

How in Linux or MacOS X to rename a bunch of files with names 20141207_190822.jpg and 20141207_190823.mp4 to this format:
2014-12-07 19.08.22.jpg
and
2014-12-07 19.08.23.mp4
?
I've found many examples how to just add something to the beginning of filename, but here I need to change the mask by inserting symbols in the middle of filenames and replacing _ with space " ".
Thank you
Try doing this :
$ rename 's#^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})#$1-$2-$3 $4.$5.$6#' *
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command :
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If you don't have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.

What does this bash script command mean (sed - e)?

I'm totally new to bash scripting but i want to solve this problem..
the command is:
objfil=`echo ${srcfil} | sed -e "s,c$,o,"`
the idea about the bash script program is to check for the source files, and check if there is an adjacent object file in the OBJ directory, if so, the rest of the program runs smoothly, if not, the iteration terminates and skips the current source file, and moves on to the next one.. it works with .c files but not on the headers, since the object filenames depend on .c files.. i want to write this command so it checks the object files not just the .c but the .h files too.. but without skipping them. i know i have to do something else too, but i need to understand what this line of command does exactly to move on. Thanks. (Sorry for my english)
UPDATE:
if test -r ${curOBJdir}/${objfil}
then
cp -v ${srcfil} ./SAVEDSRC/${srcfil}
fdone="NO"
linenums=ALL
else
fdone="YES"
err="${curOBJdir}/${objfil} is missing - ${srcfil} skipped)"
echo ${err}
echo ${err} >>${log}
fi
while test ${fdone} == "NO"
do
#rest of code ...
here is the rest of the program.. i tried to comment out the "test" part to ignore the comparison just because i only want my script to work on .h files, but without checking the e.g abc.h files has an abc.o file.. (the object file generation is needed because the end of the script there's a comparison between the hexdump of the original and modified object files). The whole script is for changing the basic types with typedefs like int to sint32_t for example.
This concrete command will substitute all c's right before line-end to o:
srcfill=abcd.c
objfil=`echo ${srcfil} | sed -e "s,c$,o,"`
echo $objfil
Output:
abcd.o
P.S. It uses a different match/replace separator: default is / but it uses ,.

Check if directory exists not working

I have a textfile (qrs.txt) which contains dir names (one per line) and on my server in the same directory as the script I have those folders with corresponding names from the text file.
This is my script:
#!/bin/bash
while read p; do
if [ ! -d "$p" ];
then
echo "ERROR $p" >> log.txt
else
echo "GOOD" >> log.txt
fi
done < qrs.txt
qrs.txt:
1992300000183805
1992300001176204
1992300002145500
1992300003104507
1992300004104902
1992300005133703
1992300006117802
1992300007144501
1992300008172803
1992300009189005
1992300010146307
1992300011151700
1992300012190007
1992300013126802
1992300014111508
1992300015193908
When that if statement is inside the loop it always returns error which is incorrect because I can see the folders exist. When I take it out of the loop and check for just 1, it works fine... When I echo $p on the same line as error, I can see the file name its checking is indeed correct.
What am I missing here..?
EDIT:
Screenshot of qrs.txt in hex mode:
http://i.snag.gy/25mqJ.jpg
RESOLVED!
My qrs.txt was in [dos] format originally but once converted to unix format using ":set ff=unix" the script worked like a charm!
Your script works fine.
I copied your script to my local machine. When I put blh blah in the qrs.txt file, I got ERROR for each time I ran your script. I ran it four times. I changed the blh blah to a valid path and I received GOOD.
The directory 1992300000183805 for instance, may be not be a valid path. You need the fully qualified path name! For example, /home/user/1992300000183805.
ERROR blh blah
ERROR blh blah
GOOD
GOOD
EDIT
Looking at #chepner comments, I recreated your problem:
Open your qrs.txt file in vi or vim. You should see ^M at the end of your lines. To remove the ^M characters at the end of all lines in vi, use:
:%s/^M//g
This should fix your problem. If not, in vim type this:
:set ff=unix
save the file.
Re-open qrs.txt in vim, then run the regex above again, or manually delete the ^M.
Or you can use perl:
perl -pi -e "s/\r/\n/g;" <file>
OK so looking at your provided file it seems those are relative directory names -- as such current directory is very important when you execute the script. Do you execute the script from its own directory or from the parent directory to all the (sub)directories shown in your example?
In other words have you tried:
cd <parent directory>
/path/to/yourscript.sh
?
Not to mention the location of qrs.txt seems to be specified relative rather than absolute path. So if there's no qrs.txt in the current directory I don't think your script would work.

Resources