I've put some common utility scripts into common.sh, which I want to use in my RPM specfile during %pre. common.sh is located in the root of the RPM package.
What I was planning to do is simply call something like source common.sh, but how can I access common.sh from the RPM during %pre?
I was able to solve this using RPM macros, the following way:
Before doing rpmbuild I have put common.spec into the SPECS folder.
common.spec
%define mymacro() (echo -n "My arg is %1 " ; sleep %1 ; echo done.)
I've added %include SPECS/common.spec as the first line of my actual spec file.
Usage example
%pre
%mymacro 5
My arg is 5 done.
Multi-line macros
Pretty fragile syntactically imo, but you can put line breaks into your macros using \. That will tell the RPM builder that it should continue parsing the macro. Given the previous macro as an example:
%define mymacro() (echo -n "My arg is %1 " ; \
sleep %1 ; \
echo done.)
This way the code will be still parsed back into a single line, hence the ; on the first and second line.
Related
I am a beginner with bash and I'm using an open source file which have this sentence ./wallet balance at the end of the file , Could any body tell me what dose it mean ?! here's the code inside the opensource file :
#!/usr/bin/env bash
C_RED="\033[31;01m"
C_GREEN="\033[32;01m"
C_YELLOW="\033[33;01m"
C_BLUE="\033[34;01m"
C_PINK="\033[35;01m"
C_CYAN="\033[36;01m"
C_NO="\033[0m"
################################################################################
### MAIN #######################################################################
################################################################################
if [[ ${#} -gt 0 ]]; then
printf "${C_RED}balance does not require any argument.\n"
printf "${C_YELLOW}usage: ${C_NO}%s\n" "balance"
exit 1
fi
./wallet balance
it means run the command called "wallet" which is to be found in the current directory "./" and provide it with a single parameter "balance".
I use the term "command" above, but in linux it could be lots of thins, for example it could be an excutable script such as a shell script, a compiled program (e.g. C) an alias etc.
I want in my makefile to retrieve all textfiles (at the moment i just got one) from a certain subfolder
and call in a loop a specific python script with each text file as an input parameter.
This is the code i currently have:
run_analysis:
#echo "Get text files"
txt_files=$(wildcard ./input/*.txt)
#echo "Current text files are:"
#echo $(txt_files)
for txt_file in $(txt_files); do \
#echo "Iteration" \
#echo $(txt_file ) \
python ./scripts/my_test_script.py $(txt_file ) ; \
done
It seems the wildcard results are not stored in the variable.
My output looks the following:
Get text files
txt_files=./input/test_text_1.txt
Current text files are:
for txt_file in ; do \
#echo "Iteration" \
#echo \
python ./scripts/my_test_script.py ; \
done
Each line in a Makefile recipe is executed in a separate shell instance by default.
Saving the files in a variable doesn't appear to serve any useful purpose anyway. Just inline the wildcard.
run_analysis:
for txt_file in ./input/*.txt; do \
python ./scripts/my_test_script.py "$$txt_file"; \
done
(Notice also how txt_file is a shell variable, not a Make variable.)
Better yet, change your Python script so it accepts a list of input files.
run_analysis:
python ./scripts/my_test_script.py ./input/*.txt
Maybe add incessant chatter with logging.debug() inside the Python script if you want to see exactly what it's doing. Unlike hard-coded echo, logging can easily be turned off once you are confident that your code works.
I'm trying to add a service under the name of $1.service, however, the service file being created is not getting the $1, instead, only .service
also there is a part where $1 needs to be pasted inside the $1.service file but it's not passing that information through as well.
this is basically how my bash script look like;
#! /bin/bash
function addService {
cat << EOF > /etc/systemd/system/$1.service
(all that service stuff here)
PIDFile=${_var}/$1.pid
EOF
}
cfg_file=~/config/"$cfg.conf"
if [ -f "$cfg_file" ]; then
. "$cfg_file"
addService $1
fi
so you run the script as ./script.sh test and it should create a service called test.service in this example, but it doesn't seem to be working properly. however, the variables like ${_conf} are passing through without any problems.
and also, do I have to use EOF specifically for this task or echo would do the job alone?
EDIT:
The config file exists and it is $1+.conf and this is the content of test.conf file;
_var=var1
and the .service file that is created passing this information without any problems. which means if $1 wasn't working, it wouldn't fetch the config file as well. but apparently, it is working.
First, you are checking for the existence of a file in ~/conf that ends in .conf? What is the value of $cfg? Does ~/conf/${cfg}.conf exist? If not, are you even going into the if clause? Using "set -x" will help debug these things.
Second, you have EOF indented. For HERE documents, the delimiter must start in the first column. You should have gotten an error when running this script about that. Something like, here-document at line X delimited by end-of-file (wanted EOF). The delimiter string can be anything (e.g. EOSD for end of service definition). It needs to start in column 1 though.
Here is what I quickly did to make sure things work.
#! /bin/bash
set -x
function addService {
cat << EOF > ./$1.service
(all that service stuff here)
PIDFile=${_conf}/$1.pid
EOF
}
cfg_file=./conf.in
if [ -f "$cfg_file" ]; then
. "$cfg_file"
addService $1
fi
Hope this helps.
The problem has been solved by changing
cat << EOF > /etc/systemd/system/$1.service
(service content here)
EOF
to
echo "
(service content here)
" > /etc/systemd/system/$1.service
Two questions about problems I'm having writing up a BASH script that uses variables. I cannot for the life of me figure this out and it is KILLING me.
1) I have the following code.
pdir=/media/The_Enforcer/ICA_Doug/Participants/RS1
cd ${pdir}
for subject in * ; do
subdir=${pdir}/${subject} ;
cd ${subdir} ;
subj= echo ${subject} | head -c-9
3dAFNItoNIFTI -prefix ICA/cleanRS_NII_${subj} RSFC_LFF_rall_${subj}+orig ;
cd ${pdir} ;
done
${subject} is a subject ID which is ########.results. For example: 1R101U1A.results. Basically my code cd's me into that directory in which is a file called RSFC_LFF_rall_1R101U1A+orig which I want to process via the code line that starts with 3dAFNItoNIFTI. Obviously I can't use ${subject} variable in that code line because it would attempt to find the file RSFC_LFF_rall_1R101U1A.results+orig which does not exist. So to fix this I made a new variable called ${subj} which, via the echo pipeline, basically cuts off the last 9 letters of ${subject} which, in effect, removes the .results. When I do this and echo ${subj} it gives me 1R101U1A which is exactly what I want.
However, the line of code that starts with 3dAFNItoNIFTI errors with the following:
FATAL ERROR: Can't open dataset 'RSFC_LFF_rall_+orig'
I have tried declaring the ${subj} variable like, five different ways (including using head, tail, cut, and colons) and I still get this error.
What am I doing wrong?
2) In attempting to define ${subj} in numerous ways I also tried this method:
${subj}= ${subject:0:8}
When I did this, the final bracket refused to close - i.e. the closing bracket did not turn the color of the opening bracket and when I attempted to run the script I got an error at that line saying 'command not found.' I checked my syntax against the examples I was following and it looks fine? Am I missing something here?
Try this:
cd "/media/The_Enforcer/ICA_Doug/Participants/RS1"
for subject in * ; do
cd $subject
subj=${subject%.results}
3dAFNItoNIFTI -prefix ICA/cleanRS_NII_${subj} RSFC_LFF_rall_${subj}+orig
cd ..
done
${subject%.results} removes .results from the end of the string.
See Shell-Parameter-Expansion.
I have recently just made this script:
if test -s $HOME/koolaid.txt ; then
Billz=$(grep / $HOME/koolaid.txt)
echo $Billz
else
Billz=$HOME/notkoolaid
echo $Billz
fi
if test -d $Billz ; then
echo "Ok"
else touch $Billz
fi
So basically, if the file $HOME/koolaid.txt file does NOT exist, then Billz will be set as $HOME/koolaid.txt. It then sucesfully creates the file.
However, if I do make the koolaid.txt then I get this
mkdir: cannot create directory : No such file or directory
Any help would be appreciated
Here is a difference between content of a variable and evaluated content...
if your variable contains a string $HOME/some - you need expand it to get /home/login/same
One dangerous method is eval.
bin=$(grep / ~/.rm.cfg)
eval rbin=${bin:-$HOME/deleted}
echo "==$rbin=="
Don't eval unless you're absolutely sure what you evaling...
Here are a couple things to fix:
Start your script with a "shebang," such as:
#!/bin/sh
This way the shell will know that you want to run this as a Bourne shell script.
Also, your conditional at the top of the script doesn't handle the case well in which .rm.cfg exists but doesn't contain a slash character anywhere in it. In that case the rbin variable never gets set.
Finally, try adding the line
ls ~
at the top so you can see how the shell is interpreting the tilde character; that might be the problem.