Which syntax for sed command with backslah? - linux

I need to change this kind of content :
"file":"\/home\/usr\/my-website.com\/data\/publication\/parameters\/21594\/1510849158.params"
into :
"file":"\/data\/publication\/parameters\/21594\/1510849158.params"
(I need to remove this string : /home\/usr\/my-website.com\ )
But the Following command has no effect, i dont know why, and syntax is a little bit complicated :
sed -i 's#/home/usr/my-website.com#/#g'
Do you have an idea ?

Related

How can I execute a cat command in a execlp in C?

I'm trying to execute commands that are passed from the terminal to argv seperated by : to be more specific cat nevermind : grep left : wc -c.
tabCommand is an array that contains each command so cat nevermind,grep left,wc -c
With printf I can confirm that tabCommand[i-1] is indead equal to cat nevermind but the output I get is Error: No such file or directory
if (execl(tabCommande[i-1],tabCommande[i-1], (char *)NULL) == -1) {
error_and_exit();
}
If someone can help me find the issue I would really appreciate it.
With the comments I got in my post I managed to find my problem
execlp("/bin/sh","sh","-c",tabCommande[i], (char *)NULL) works because I need to use the full path.
If I do execlp(tabCommande[i],tabCommande[i], (char *)NULL) it won't work because im not using the full path of each command so simply giving cat to execlp won't work.
found this answer thanks to waltinator I'm new to stack so i dont know how to give you the credit

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)

How to Use "sed" Bash Command

I am having difficulties replacing a string in c program file with the content of a variable in a bash file. The idea is to copy the address of Linux kernel sys_call_table and then use it in my C program to intercept system calls. I found a couple of examples online, but none of them have worked for me so far. So any help will be greatly appreciated.
Here is the content of my bash file, "bashFile.sh"
TABLE=$(grep sys_call_table /boot/System.map-$(uname -r) |awk '{print $1}')
sed -i "s/myTABLE/{$TABLE}/g" my_LKM.c
When I run "sudo sh bashFile.sh" command, nothing happen. The string myTable in C file does not get replaced. However, when I try the following variation:
TABLE=$(grep sys_call_table /boot/System.map-$(uname -r) |awk '{print $1}')
sed -i 's/myTABLE/{$TABLE}/g' my_LKM.c
the myTABLE string get replaced with {$TABLE} instead of the content of the variable TABLE (sys_call_table address). I tried debugging with "echo $TABLE" to see if TABLE content is the address of sys_call_table and it worked. So, I concluded that the problem might be the syntax of sed command. However, I do not know how to fix it at this time. Thank you in advance for your help.
P.S. Below is the content of myLKM.c file:
unsigned long *sys_call_table;
sys_call_table = (unsigned long *)myTABLE;
First of all, don't use sudo to run this. It doesn't magically make things work. If you need root privileges to edit source code you're working on, you have serious problems with your development setup.
Use double-quotes if you want stuff to expand inside them. e.g.
TABLE=$(awk '/sys_call_table/ {print $1}' /boot/System.map-$(uname -r))
sed -i "s/myTABLE/${TABLE}UL/g" my_LKM.c
As karakfa suggested, make sure you try this without -i first. His other suggestion, of expanding $TABLE in an unquoted context, instead of inside double quotes, was terrible, though. You need the entire string to be part of the same sed arg, so just ending the single quotes is bad.
Also, I guess he misread {$TABLE} for ${TABLE}, and simplified to $TABLE. Or actually, from the context of your question, it looks like you want a plain numeric constant, not wrapped in braces. So you should use $TABLE.
Actually, you should use ${TABLE}UL so it's an unsigned-long integer literal. Without those modifiers, an address that didn't fit in the low32 would probably get mangled before the cast to a pointer type was applied.
You have to escape the quotes
sed -i 's/myTABLE/'$TABLE'/g' my_LKM.c
obviously try first NOT in-place (remove -i)

use text and use as command?

i whant to build a script to extract the word from a file in a command:
for example:
in my file i keep my username , how can i export the username without knowing the username as part of my command , which my command is : suspend here should be the word exported?
From my file which has the text inside "JOHN" how can i tell the command : suspend that the username is JOHN ?
how can i cat/grep/sed the text from the file as part of command :
something like suspend |cat file , i have try that but the suspend command does not take the username , so there should be something different .
my ideea where like this
#!/bin/sh
username=cat my-file;
suspend $username ,
but i had no succes , because my $username is not display from the cat , the cat is shows it but thats all that is doing.
Use the following syntax
username=$(cat my-file)
you might also use backquotes like
username=`cat my-file`
but the $(...) notation is preferable: it can be nested and is more readable.
Please read the Advanced Bash Scripting Guide (it will teach you a lot, even if it can be criticized)
Your usage of suspend is incorrect, and I don't understand what you want to do with it. What would suspending a user mean to you? Perhaps you want to use pgrep or pkill (to kill all the processes of that user, which I feel is too harsh)...?
The command to read a line is, shockingly, named "read":
$ cat file
John
$ IFS= read -r username < file
$ echo "$username"
John
"cat" is the command to concatenate files.
Setting IFS= and using the -r arg to read are so that read handles leading white space and/or backslashes as-is rather then interpreting them. If you KNOW your file can never contain those or you want read to strip leading white space and/or interpret backslashes than you can get rid of those.

Bash Shell - The : Command

The colon command is a null command.
The : construct is also useful in the conditional setting of variables. For example,
: ${var:=value}
Without the :, the shell would try to evaluate $var as a command. <=???
I don't quite understand the last sentence in above statement. Can anyone give me some details?
Thank you
Try
var=badcommand
$var
you will get
bash: badcommand: command not found
Try
var=
${var:=badcommand}
and you will get the same.
The shell (e.g. bash) always tries to run the first word on each command line as a command, even after doing variable expansion.
The only exception to this is
var=value
which the shell treats specially.
The trick in the example you provide is that ${var:=value} works anywhere on a command line, e.g.
# set newvar to somevalue if it isn't already set
echo ${newvar:=somevalue}
# show that newvar has been set by the above command
echo $newvar
But we don't really even want to echo the value, so we want something better than
echo ${newvar:=somevalue}.
The : command lets us do the assignment without any other action.
I suppose what the man page writers meant was
: ${var:=value}
Can be used as a short cut instead of say
if [ -z "$var" ]; then
var=value
fi
${var} on its own executes the command stored in $var. Adding substitution parameters does not change this, so you use : to neutralize this.
Try this:
$ help :
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds.

Resources