Script:
#!/bin/bash
mydir="/bamboo/artifacts"
cd "$mydir"
job="-JOB1"
for dir in */
do
bambooplan=`echo $dir | sed 's/\/$//g'`$job
echo $bambooplan
done
Output:
LERST-TSTREDAPIDB2WIN4-JOB1
LERST-TSTREDAPIDB2WIN5-JOB1
CA-TSTALLSQLWIN3-JOB1
CE-CSW-JOB1
CE-SNAP-JOB1
I want to pass this to a file. I tried the commands below and it gives permission denied error. Could someone help me figure what I miss here?
echo $bambooplan > result.txt
$bambooplan > result.txt
I tested your code with the first 'writing line' (echo $bambooplan > result.txt) and worked perfect for me, only i would change echo $bambooplan > result.txt to echo $bambooplan >> result.txt, otherwise, you will overwrite the entire file in each iteration, keeping only the last one.
Be aware that your code writes INSIDE /bamboo/artifacts and you wrote the file path as /bamboo/artifacts, an absolute path, not a relative one -> bamboo/artifacts, so, maybe, you're trying to write in a folder where you don't have writing permissions and need to chmod them.
Redirect just to your defined file by using > as you specified or >> if file already already exists and contains data. Regarding to the permission, just exchange script's rights thru chmod command.
for instance :
chmod 755 scriptname
Same process to change output file's permission.
Related
I have several Name*.txt files in /home/user/my/path/to/my/data/, among other files with different extensions. I would like to loop over them, then use the individual file names in the code, therefore common solutions like this won't work, since the varible '$f', within each loop, stores the whole path together with the file name. I need them separately, to perform something like the "example taks" below. My attempts:
Attempt #1:
#!/bin/bash
datapath="/home/user/my/path/to/my/data/"
outpath="/home/user/my/path/to/my/outputs/"
for f in $(ls $datapath"Name*.txt"); do
echo $f
...
cp $datapath$f $outpath"example_task"${f:0:10}
done
This didn't work:
ls: cannot access /home/user/my/path/to/my/data/Name*.txt: No such file or directory.
Although running ls /home/user/my/path/to/my/data/Name*.txt on the terminal works perfectly fine. I can't understand why.
Attempt #2:
#!/bin/bash
datapath="/home/user/my/path/to/my/data/"
outpath="/home/user/my/path/to/my/outputs/"
for f in $datapath"Name*.txt"; do
echo $f
...
cp $datapath$f $outpath"example_task"${f:0:10}
done
Here, each $f contains the full list of files ls Name*.txt would normally return, and not one at a time as one would expect.
How do I do this? Any suggestions will be much appreciated.
Maybe I am on the wrong path here but this worked for me.
#!/bin/bash
datapath="/home/user/my/path/to/my/data/"
outpath="/home/user/my/path/to/my/outputs/"
cd $datapath
for f in ./*.txt; do
file=$(echo $f | cut -d '/' -f 2)
echo $file
...
cp $f $outpath"example_task"$file
done
cd
I've spent the past hour trying to find a way around this before asking but to no avail so I'm asking.
I am trying to make a simple script that will take the name for a file and then generate a generic blank html template for me.
#!/bin/bash
blank=/home/sithyrys/Documents/scripts/blank.html
echo "Enter file name with no extensions:"
read fileName
fileName+=.html
echo $fileName
touch $fileName
$blank >> $fileName
When I comment out the path the code runs with no error message but then it's not pulling the template and it makes a blank page. The error message in question is:
./basicHTMLTemplate.sh: line 9: /home/sithyrys/Documents/scripts/blank.html: Permission denied
Edit: shebang line copied wrong that was correct already
>> does not copy a file; it appends the output of the command that precedes it to the file named following it. You need to use the cat command to actually "push" the contents of blank.html into the new file.
cat "$blank" >> "$fileName"
As written, your code accommodates the possibility that $fileName already exists and appends the contents of $blank without overwriting the existing file. In practice, it doesn't make much sense to append the template to the end of an existing file, so you probably just want to make a copy of the template.
#!/bin/bash
blank=/home/sithyrys/Documents/scripts/blank.html
echo "Enter file name with no extensions:"
read fileName
fileName+=.html
echo $fileName
cp "$blank" "$fileName"
(or, to guard against overwriting an existing file,
[[ -f "$fileName" ]] || cp "$blank" "$fileName"
)
I want to read all the files in a particular directory, and I want to read it one by one.
Here's what i've done so far. ls successfully get all the files from a specified directory, but could not give the file names to me one by one. It Echos the files one time only. I want to get it one by one because I need to do some parsing and use it somehwere.
#!/bin/sh
echo Content-type: application/json
echo ""
for output in "ls /home/myComputer/Desktop/*";
do
echo $output
done
You can do
for output in /home/myComputer/Desktop/*
do
echo $output
done
If there's any risk that /home/myComputer/Desktop is empty, please follow #JIDs advice in the comments below.
The next solution also works when the dir is empty.
ls /home/myComputer/Desktop/ 2>/dev/null |while read -r output; do
echo ${output}
done
This construction is nice to know, for when you want to split the input lines in some fields:
cat someFile | while read -r field1 field2 remainingfields; do
This probably quite basic but I have spent whole day finding an answer without much success.
I have an executable script that resides in ~/Desktop/shell/myScript.sh
I want a single line command to run this script from my terminal that outputs to a new directory in same directory where the script is located no matter what my present working directory is.
I was using:
mkdir -p tmp &&
./Desktop/shell/myScript.sh|grep '18x18'|cut -d":" -f1 > tmp/myList.txt
But it creates new directory in present working directory and not on the target location.
Any help would be appreciated.
Thanks!
You could solve it in one line if you pre-define a variable:
export LOC=$HOME/Desktop/shell
Then you can say
mkdir -p $LOC/tmp && $LOC/myScript.sh | grep '18x18' | cut -d":" -f1 > $LOC/tmp/myList.txt
But if you're doing this repeatedly it might be better long-term to wrap myScript.sh so that it creates the directory, and redirects the output, for you. The grep and cut parameters, as well as the output file name, would be passed as command-line arguments and options to the wrapper.
How about this:
SCRIPTDIR="./Desktop/shell/" ; mkdir "$SCRIPTDIR/tmp" ; "$SCRIPTDIR/myScript.sh" | grep '18x18' | cut -d ":" -f 1 > "$SCRIPTDIR/tmp/myList.txt"
In your case you have to give the path to the script anyway. If you put the script in the path where it is automatically searched, e.g. $HOME/bin, and you can just type myScript.sh without the directory prefix, you can use SCRIPTDIR=$( dirname $( which myScript.sh ) ).
Mixing directories with binaries and data files is usually a bad idea. For temporary files /tmp is the place to go. Consider that your script might become famous and get installed by the administrator in /usr/bin and run by several people at the same time. For this reason, try to think mktemp.
YOUR SCRIPT CAN DO THIS FOR YOU WITH SOME CODES
Instead of doing this manually from the command line and who knows where you will move your script and put it. add the following codes
[1] Find your script directory location using dirname
script_directory=`dirname $0`
The above code will find your script directory and save it in a variable.
[2] Create your "tmp" folder in your script directory
mkdir "$script_directory/tmp 2> /dev/null"
The above code will make a directory called "tmp" in your script directory. If the directory exist, mkdir will not overwrite any existing directory using this command line and gave an error. I hide all errors by "2> /dev/null"
[3] Open your script and modify it using "cut" and then redirect the output to a new file
cat "$0"|grep '18x18'|cut -d":" -f1 > "$script_directory"/tmp/myList.txt
I want to write a script that do specific thing:
I have a txt file e.g.
from1/from2/from3/apple.file;/to1/to2/to3;some not important stuff
from1/from2/banana.file;/to1/to5;some not important stuff
from1/from10/plum.file;/to1//to5/to100;some not important stuff
Now i want to copy file from each line (e.g. apple.file), from original directory tree to new, non existing directories, after first semicolon (;).
I try few code examples from similar questions, but nothing works fine and I'm too weak in bash scripting, to find errors.
Please help :)
need to add some conditions:
file not only need to be copy, but also rename. Example line in file.txt:
from1/from2/from3/apple.file;to1/to2/to3/juice.file;some1
from1/from2/banana.file;to1/to5/fresh.file;something different from above
so apple.file need to be copy and rename to juice.file and put in to1/to2/to3/juice.file
I think thaht cp will also rename file but
mkdir -p "$to"
from answer below will create full folder path with juice.file as folder
In addidtion after second semicolon in each line will be something different, so how to cut it off?
Thanks for all help
EDIT: There will be no spaces in input txt file.
Try this code..
cat file | while IFS=';' read from to some_not_important_stuff
do
to=${to:1} # strip off leading space
mkdir -p "$to" # create parent for 'to' if not existing yet
cp -i "$from" "$to" # option -i to get a warning when it would overwrite something
done
Using awk
(run the awk command first and confirm the output is fine, then add |sh to do the copy)
awk -F";" '{printf "cp %s %s\n",$1,$2}' file |sh
Using shell (get updated that need manually create folder, base on alfe's
while IFS=';' read from to X
do
mkdir -p $to
cp $from $to
done < file
I had this same problem and used tar to solve it! Posted here:
tmpfile=/tmp/myfile.tar
files="/some/folder/file1.txt /some/other/folder/file2.txt"
targetfolder=/home/you/somefolder
tar --file="$tmpfile" "$files"
tar --extract --file="$tmpfile" --directory="$targetfolder"
In this case, tar will automatically create all (sub)folders for you! Best,
Nabi