So I recently made a simple bash script which I want to share with other people. So I tried making the script search for files in a folder in which other people will place them, which worked fine for a few files.
Though I have a command in my script that doesn't accept the variables, they get passed, but they don't get replaced in the command. So instead of using a variable I had to use the path to the files, which are only on my computer.
And since I want everyone to use that script I need a way to replace these paths. So I thought of this:
Use the same command that found the other file to find this one. Then replace the path to the file in the script with the path that was found by the command.
Though my problem is, that I haven't found something that is what I wanted. Here's my code:
#!/bin/bash
#variables that store the paths
dtree=$(find Downgrade -type f -iname DeviceTree*)
ramdisk=$(find Downgrade -type f -iname *.dmg)
kernel=$(find Downgrade -type f -iname kernelcache*)
#the execution of the command. (Using normal EOF without ”” doesn’t replace the strings.)
./irecovery -s <<"EOF" >/dev/null
/send Downgrade/DeviceTree.n90ap.img3 #This needs to be replaced by $dtree
devicetree
/send Downgrade/048-2441-007.dmg #This needs to be replaced by $ramdisk
ramdisk
/send Downgrade/kernelcache.release.n90 #This needs to be replaced by $kernel
bootx
/exit
EOF
Though I am not sure about your question, you can try replacing string using sed
sed "s/old/new/g"
Related
The problem is that when I use this part of a script, it works fine on a linux environment and in the rest of the code it returns the list of files. However, when using CMDER (Full version) on Windows 10, it simply returns "no .txt files found". So no .txt files found in defined path even though I have 3 .txt files there. I even tried MobaXterm and got the same results.
path=$1
#files with read permission in path
count=`find $path -type f -name '*.txt' -perm /a=r`
# at least one file found
if [ "X${count}" = "X" ]
then
echo 'no .txt files found'
exit 1
fi
Please note that I'm a beginner in shell scripting and really want to learn so any advice is greatly appreciated!
I fixed the problem by adding the following line at the end of user_profile inside the config folder
set "PATH=%GIT_INSTALL_ROOT%\usr\bin;%PATH%"
NOTE: there are other ways to fix it by this seemed to be the simplest way for me.
Are you sure your script works at all? Equality operator in Bash is ==, not =.
See String comparison in Bash
I worte a simple script to check if there are some files exist (endded with .txt) in the dirctoey older than 6 hours, after the check to send an email.
The scripte isnt working well and as expected, I ask you if there's some simpler and more powerful way to do it? Basically it just needs to check if file eneded with .txt exists and older than 6hours, if yes an email should be sent.
This is my script
#!/bin/bash
DATE=`date +%Y.%m.%d-%H.%M`
HOSTNAME='host'
BASEDIR=`/usr/local/se/work/jobs/`
LOGFILE=`/usr/local/se/work/jobs/logs/jobs.log`
VERTEILER="anyemail"
# Functions
#
# function check if the jobs are exists
'find ${BASEDIR} -name "*.txt" -nmin +354' 2>$1 >>$LOGFILE
#function mail
cat << EOF | mailx -s "Example ${HOSTNAME} jobs `date +%Y.%m.%d-%H.%M`" -a ${LOGFILE} ${VERTEILER}
Hi,
Please check the Jobs.
Details :
`ls -ltr /usr/local/se/work/jobs/`
------------ END ----------------------------------------
.
Thank you
To redirect STDERR to STDOUT use 2>&1, you are doing it wrong with 2>$1
Also, the correct parameter for find is -mmin not -nmin like you have in your code.
Further more you have syntax errors like here:
BASEDIR=`/usr/local/se/work/jobs/`
LOGFILE=`/usr/local/se/work/jobs/logs/jobs.log`
What you mean to type is:
BASEDIR='/usr/local/se/work/jobs/'
LOGFILE='/usr/local/se/work/jobs/logs/jobs.log'
When you use backticks bash tries to execute COMMAND, you are using it the right way here:
DATE=`date +%Y.%m.%d-%H.%M`
You are also not closing the heredoc <<EOF, you need EOF on the last line of the script.
And lose the '' surrounding the find command.
You should pay attention to what bash says, it should prompt lots of errors, try to run the script manually to see them if you are using this via cron.
Did you even try to run this script?
The grave accent (`) in BASEDIR and LOGFILE means the shell will try to evaluate them as commands which fails. You don't generally need quotes around strings in shell scripts, although it may be considered good practice to use double quotes (").
HOSTNAME=host
BASEDIR=/usr/local/se/work/jobs
LOGFILE=${LOGFILE}/logs/jobs.log
VERTEILER=anyemail
The switch to search files by minutes in called mmin and not nmin -- again that should have given you an error. And the math is wrong, if you want 6 hours then it's 6 * 60 = 360 minutes.
find ${BASEDIR} -name *.txt -mmin +360
You are redirecting stderr to the first input parameter. (2>$1) Are you expecting error output from this command, could you explain what is going on here?
And then you append that to LOGFILE which is in a directory that may not exist. mkdir -p is a good choice for creating folders in scripts because it creates parent directories when needed and won't complain if the folder already exists. So do something along the lines of
mkdir -p /usr/local/se/work/jobs/logs
I am trying to figure out how to write a shell script for Solaris 10 that finds all of the ownerless files on the box using an if statement, prints the file names and locations and assigns them the root owner if they are ownerless.
Fairly new with unix in general and shell scripting.
Please help.
Something like this should work:
find / -nouser -exec echo chown root '{}' \;
Once you are satisfied with the output, remove the echo from the line above and re-run.
Dissecting the command above:
find - The command you are executing
/ - Start looking for files at / (so look at all files)
-nouser - Find only files whose numeric user ID doesn't have a corresponding entry in /etc/passwd
-exec - Run the following command for all of the files that we found based on the previous conditions
echo chown root '{}' \; - The command to run for each of the matched files. {} is replaced with the full filename and the ; is escaped so that find sees it rather than the shell seeing it as an end-of-command marker.
The echo is there so that you can validate that the appropriate commands will run before running potentially the chown command which might screw things up.
So you run the above once, make sure that the commands it prints out are good, and then you re-run the find command above but you remove the echo so that chown is actually executed instead of just bring printed out.
I'm new to bash and have encountered a problem i can't solve. The issue is i need to use find -name with a name defined as a variable. Part of the script:
read MYNAME
find -name $MYNAME
But when i run the script, type in '*sh' for read, there are 0 results.
However, if i type directly in the terminal:
find -name '*sh'
it's working fine.
I also tried
read MYNAME
find -name \'$MYNAME\'
with typing *sh for read and no success.
Can anyone help me out?
Most probably
read MYNAME
find -name "$MYNAME"
is the version you are looking for. Without the double quotes " the shell will expand * in your *sh example prior to running find that's why your first attempt didn't work
You probably want
find -name "$MYNAME"
since this prevents $MYNAME from being subject to bash's pathname expansion (a.k.a. "globbing"), resulting in *sh being passed intact to find. One key difference is that globbing will not match hidden files such as .ssh, whereas find -name "*sh" will. However since you don't define the expected behaviour you are seeking, it's hard to say what you need for sure.
I am reading in a list of file names:
*.txt *.xml
which are space delimited. I read this into a variable in my ksh script, and I want to be able to manipulate it before putting each of them into a find command. The problem is, as soon as I do anything with the variable (for instance, breaking it into an array), the * resolves into filenames that are in my script's directory. What I want is for the *.txt to remain unchanged, so I can put that into my find command.
How do I do this? Unfortunately, I'm at work and can't just use perl or some other language.
set -f
turns off globbing in ksh, so * and ? characters are not expanded (globbed).
what's wrong with
'*.txt' '*.xml'
? . Else you have to show us more of your issues. Maybe edit your post to include a small test case that illustrates your problem, plus the desired output or intermediate values.