How exactly does ${!OPTIND} work in getopts?
I wrote a noscript to try and understand using long arguments in getopts, but don't really get what the
while getopts 'abc:-:' OPT; do
echo -e "${OPT}\t${OPTIND}\t${OPTARG}"
case ${OPT} in
-)
case ${OPTARG} in
version)
echo "OPT: ${OPT}"
echo "OPTARG: ${OPTART}"
echo "OPTIND: ${OPTIND}"
echo "!OPTIND: ${!OPTIND}"
(( OPTIND++ ))
;;
esac
esac
done
Output:
$ ./test.sh -ab -c FOO --version 1.23
a 1
b 2
c 4 FOO
- 5 version
OPT: -
OPTARG:
OPTIND: 5
!OPTIND: 1.23
So, from what I can gather from the output,
​
>If the first character of "PARAMETER" is an exclamation point, Bash uses the value of the variable formed from the rest of "PARAMETER" as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of "PARAMETER" itself.
Is this some kind of indirect expansion on an array?
https://redd.it/xkoi3u
@r_bash
I wrote a noscript to try and understand using long arguments in getopts, but don't really get what the
! operator is doing. If ${OPTIND} is the index value of the next argument, then how exactly does ${!OPTIND} work to get the value for --version?while getopts 'abc:-:' OPT; do
echo -e "${OPT}\t${OPTIND}\t${OPTARG}"
case ${OPT} in
-)
case ${OPTARG} in
version)
echo "OPT: ${OPT}"
echo "OPTARG: ${OPTART}"
echo "OPTIND: ${OPTIND}"
echo "!OPTIND: ${!OPTIND}"
(( OPTIND++ ))
;;
esac
esac
done
Output:
$ ./test.sh -ab -c FOO --version 1.23
a 1
b 2
c 4 FOO
- 5 version
OPT: -
OPTARG:
OPTIND: 5
!OPTIND: 1.23
So, from what I can gather from the output,
--version is at the fourth index, and 1.23 would be at the fifth index (which is why we use (( OPTIND++ )) to skip to the next argument, which would subsequently be positioned at the sixth index, if another argument were present). But how exactly is ! getting the value at the fifth index? From the documentation on shell expansion, it seems that this has something to do with indirect expansion:​
>If the first character of "PARAMETER" is an exclamation point, Bash uses the value of the variable formed from the rest of "PARAMETER" as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of "PARAMETER" itself.
Is this some kind of indirect expansion on an array?
https://redd.it/xkoi3u
@r_bash
How to write a noscript to capture the banner of a ssh session ? (bash or expect?)
I’m trying to write a noscript that would open a ssh session, read the « welcome banner », parse it and store it. I started using expect and so far I’m able to do the basics: open the session and later close it. What I miss is the way to « capture » the banner (where it says last login from ip, date, etc…). The goal is for the noscript to connect to multiple server, extract the ip/date of the last login and store it (in a cab or a db, not sure yet). Any idea ?
https://redd.it/xkovuv
@r_bash
I’m trying to write a noscript that would open a ssh session, read the « welcome banner », parse it and store it. I started using expect and so far I’m able to do the basics: open the session and later close it. What I miss is the way to « capture » the banner (where it says last login from ip, date, etc…). The goal is for the noscript to connect to multiple server, extract the ip/date of the last login and store it (in a cab or a db, not sure yet). Any idea ?
https://redd.it/xkovuv
@r_bash
reddit
How to write a noscript to capture the banner of a ssh session ?...
I’m trying to write a noscript that would open a ssh session, read the « welcome banner », parse it and store it. I started using expect and so far...
Is it faster to run mkdir every time than to put it in an if statement to check if the dir exists?
I posted about a noscript here recently and a lot of people commented on running mkdir in an if statement, that I shouldn't do it. I understand that mkdir just doesn't need that because it will return 0. What if this was your bashrc and you had some custom directories that it required? Should you let mkdir return 0 every time bash runs or is it faster to just check for the dir?
So what is faster? 'if [ -d dir ];' or 'mkdir dir' assuming 'dir' exists? Is there an even faster way to handle dir checking? Speed doesn't really matter in the single instance that it will run and the dirs don't exist, only in every instance after.
https://redd.it/xkqhfd
@r_bash
I posted about a noscript here recently and a lot of people commented on running mkdir in an if statement, that I shouldn't do it. I understand that mkdir just doesn't need that because it will return 0. What if this was your bashrc and you had some custom directories that it required? Should you let mkdir return 0 every time bash runs or is it faster to just check for the dir?
So what is faster? 'if [ -d dir ];' or 'mkdir dir' assuming 'dir' exists? Is there an even faster way to handle dir checking? Speed doesn't really matter in the single instance that it will run and the dirs don't exist, only in every instance after.
https://redd.it/xkqhfd
@r_bash
reddit
Is it faster to run mkdir every time than to put it in an if...
I posted about a noscript here recently and a lot of people commented on running mkdir in an if statement, that I shouldn't do it. I understand that...
Change string format
Hi all
I want to take DATEORIGINAL="202209" and set DATENEW to 2022-09. I used a case and it worked but that's very static.
Originally it's 20220921 so I did a DATEORIGINAL=${date long::-2} to strip day. Now I want to add that hyphen.
What's the best way to go about it?
https://redd.it/xkoo99
@r_bash
Hi all
I want to take DATEORIGINAL="202209" and set DATENEW to 2022-09. I used a case and it worked but that's very static.
Originally it's 20220921 so I did a DATEORIGINAL=${date long::-2} to strip day. Now I want to add that hyphen.
What's the best way to go about it?
https://redd.it/xkoo99
@r_bash
reddit
Change string format
Hi all I want to take DATEORIGINAL="202209" and set DATENEW to 2022-09. I used a case and it worked but that's very static. Originally it's...
Find string 3 times in the file
Staff,
I created the function below that is locating in the directory files "/X/X/logs" the string I was looking for.
What I need now is for the noscript to verify that the string was found 3 or more times inside the file. If show up 1 or 2, shouldn't let me know.
Does anyone know how I could do that?
Grateful for any idea.
function check\_logs {
FLAG=\`find /X/X/logs -type f -exec grep -l 'String' {} +;\`
if \[\[ ! -z ${FLAG} \]\]; then
printf "Found:\\n\\n${FLAG}\\n"
else
echo "Not found"
fi
}
EDIT: formatting
​
https://redd.it/xkvkcr
@r_bash
Staff,
I created the function below that is locating in the directory files "/X/X/logs" the string I was looking for.
What I need now is for the noscript to verify that the string was found 3 or more times inside the file. If show up 1 or 2, shouldn't let me know.
Does anyone know how I could do that?
Grateful for any idea.
function check\_logs {
FLAG=\`find /X/X/logs -type f -exec grep -l 'String' {} +;\`
if \[\[ ! -z ${FLAG} \]\]; then
printf "Found:\\n\\n${FLAG}\\n"
else
echo "Not found"
fi
}
EDIT: formatting
​
https://redd.it/xkvkcr
@r_bash
reddit
Find string 3 times in the file
Staff, I created the function below that is locating in the directory files "**/X/X/logs**" the string I was looking for. What I need now is for...
Help😅
I’ve used this sed command: sed-i's/\/\n/g;s/\>/\n/g;' <TextFile>
Is there any way to do the same without using sed, for example «tr» command?
https://redd.it/xkwfhn
@r_bash
I’ve used this sed command: sed-i's/\/\n/g;s/\>/\n/g;' <TextFile>
Is there any way to do the same without using sed, for example «tr» command?
https://redd.it/xkwfhn
@r_bash
reddit
Help😅
I’ve used this sed command: sed-i's/\/\n/g;s/\>/\n/g;' <TextFile> Is there any way to do the same without using sed, for example «tr» command?
Recording footage from a press of a button!
Hi yall,
Just a heads up, I am using a Raspberry Pi 3. I am attempting to record footage from my webcam through the raspberry pi. I am interested in having this done through terminal - no gui. The code below works well but I have to manually stop the code and it always saves as "out0001.mkv".
>ffmpeg -f v4l2 -standard PAL -threadqueuesize 512 -i /dev/video0 -f alsa -threadqueuesize 512 -i hw:0,0 -vcodec libx264 -preset superfast -crf 25 -s 640x480 -r 25 -aspect 16:9 -acodec libmp3lame -b:a 128k -channels 2 -ar 48000 out0001.mkv
so i have a question: Is there a way to set it up so that I could press a physical button that's connected to the Raspberry Pi's GPIO so it can start and stop a recording?
Another question: is it possible to have it save in a new file every time i finish a recording automatically so what I mean by this is that the first file would be out0001.mkv
second round of recording, it checks if out0001.mkv exists, if it does, it would instead save the new footage as out0002.mkv and if both out0001.mkv and out0002.mkv exists - it would save as out0003.mkv and so forth.
Any help would be appreciated. Thanks :D
https://redd.it/xkyrhd
@r_bash
Hi yall,
Just a heads up, I am using a Raspberry Pi 3. I am attempting to record footage from my webcam through the raspberry pi. I am interested in having this done through terminal - no gui. The code below works well but I have to manually stop the code and it always saves as "out0001.mkv".
>ffmpeg -f v4l2 -standard PAL -threadqueuesize 512 -i /dev/video0 -f alsa -threadqueuesize 512 -i hw:0,0 -vcodec libx264 -preset superfast -crf 25 -s 640x480 -r 25 -aspect 16:9 -acodec libmp3lame -b:a 128k -channels 2 -ar 48000 out0001.mkv
so i have a question: Is there a way to set it up so that I could press a physical button that's connected to the Raspberry Pi's GPIO so it can start and stop a recording?
Another question: is it possible to have it save in a new file every time i finish a recording automatically so what I mean by this is that the first file would be out0001.mkv
second round of recording, it checks if out0001.mkv exists, if it does, it would instead save the new footage as out0002.mkv and if both out0001.mkv and out0002.mkv exists - it would save as out0003.mkv and so forth.
Any help would be appreciated. Thanks :D
https://redd.it/xkyrhd
@r_bash
reddit
Recording footage from a press of a button!
Hi yall, Just a heads up, I am using a Raspberry Pi 3. I am attempting to record footage from my webcam through the raspberry pi. I am interested...
Symlink the contents of a directory, in to another, preserving structure.
Hi,
I've been trying to figure this out for a while and my brain just hurts...like I've literally given myself a headache trying to figure this out as it used to be junk I could handle in five minutes. (brain-fog sucks)
I run a Jekyll based site that's built every repo push with a githook. That part works fine. What I'm having issue with is some "extra" stuff I do after the jekyll rendering related to symlinking files. When Jekyll builds the site, it naturally just wipes out the root directory and builds one from scratch.
My problem is I do not keep all of my stuff in the repo. I have a lot of images and other content that I clearly don't want in the repo. So what I want to do is setup a directory that contains all the non jekyll content in the usual folder structure. So if my site is in /var/www/sitename then I might keep the static content in /var/www/sitename-static. So I came up with the easy solution:
​
>ln -s /var/www/sitename-static/* /var/www/sitename/
This accomplishes what I want...mostly. The contents and all the directories in -static show up in sitename perfectly...except in cases where the folder already exists. It doesn't just error out on trying to symlink the folder; but it ignores all the files.
Right now I feel like I'd have to for-loop every folder in every sub-directory to make this work. I feel like I used to know the solution but my mush-brain can't retrieve it anymore.
https://redd.it/xl64tm
@r_bash
Hi,
I've been trying to figure this out for a while and my brain just hurts...like I've literally given myself a headache trying to figure this out as it used to be junk I could handle in five minutes. (brain-fog sucks)
I run a Jekyll based site that's built every repo push with a githook. That part works fine. What I'm having issue with is some "extra" stuff I do after the jekyll rendering related to symlinking files. When Jekyll builds the site, it naturally just wipes out the root directory and builds one from scratch.
My problem is I do not keep all of my stuff in the repo. I have a lot of images and other content that I clearly don't want in the repo. So what I want to do is setup a directory that contains all the non jekyll content in the usual folder structure. So if my site is in /var/www/sitename then I might keep the static content in /var/www/sitename-static. So I came up with the easy solution:
​
>ln -s /var/www/sitename-static/* /var/www/sitename/
This accomplishes what I want...mostly. The contents and all the directories in -static show up in sitename perfectly...except in cases where the folder already exists. It doesn't just error out on trying to symlink the folder; but it ignores all the files.
Right now I feel like I'd have to for-loop every folder in every sub-directory to make this work. I feel like I used to know the solution but my mush-brain can't retrieve it anymore.
https://redd.it/xl64tm
@r_bash
reddit
Symlink the contents of a directory, in to another, preserving...
Hi, I've been trying to figure this out for a while and my brain just hurts...like I've literally given myself a headache trying to figure this...
Script to change ECS tasks desireCount to 0 and then start it
Hi guys,
Since ECS services cant be stopped (tasks can) I need to write a noscript to place it inside a buildspec.yml which is also inside a Codepipeline. The noscripts need to grab an ECS service and update all its tasks with DesireCount 0 and then write another one, that replaces the DesireCount = 1
The idea is to have desirecount value, hardcoded on the noscript
So far I haves this
\# aws ecs update-service --desired-count 0 --cluster "ecs-my-ClusterName" --service "service-my-ServiceName-zxzxz"
#!/bin/bash
echo "Modify desire count"
for service in $(ecs_services); do
aws ecs update-service --cluster ${ecsClusterName} --service "$service" --desired-count 0 --no-cli-pager > /dev/null;
done
#Here comes the part to start tasks
Does it make any sense? Its been awhile since my last shell noscript
https://redd.it/xl63ma
@r_bash
Hi guys,
Since ECS services cant be stopped (tasks can) I need to write a noscript to place it inside a buildspec.yml which is also inside a Codepipeline. The noscripts need to grab an ECS service and update all its tasks with DesireCount 0 and then write another one, that replaces the DesireCount = 1
The idea is to have desirecount value, hardcoded on the noscript
So far I haves this
\# aws ecs update-service --desired-count 0 --cluster "ecs-my-ClusterName" --service "service-my-ServiceName-zxzxz"
#!/bin/bash
echo "Modify desire count"
for service in $(ecs_services); do
aws ecs update-service --cluster ${ecsClusterName} --service "$service" --desired-count 0 --no-cli-pager > /dev/null;
done
#Here comes the part to start tasks
Does it make any sense? Its been awhile since my last shell noscript
https://redd.it/xl63ma
@r_bash
reddit
Script to change ECS tasks desireCount to 0 and then start it
Hi guys, Since ECS services cant be stopped (tasks can) I need to write a noscript to place it inside a buildspec.yml which is also inside a...
New to bash with a simple question.
Say I’m already in a folder and just want to see all files that are 4 character long in the name. What command can do that?
https://redd.it/xladhp
@r_bash
Say I’m already in a folder and just want to see all files that are 4 character long in the name. What command can do that?
https://redd.it/xladhp
@r_bash
reddit
New to bash with a simple question.
Say I’m already in a folder and just want to see all files that are 4 character long in the name. What command can do that?
Working on browser agnostic bookmarks, shellcheck no help.
This seems to work fine.
https://paste.debian.net/1254702/
But shellcheck complains about
https://i.imgur.com/M3OoBmf.jpg
Ignore shellcheck? I mean it works as I want, I think. Haven't tested spaces in denoscription much, which I'd very much like to have, but it does seem to work as is.
https://redd.it/xlbtgc
@r_bash
This seems to work fine.
https://paste.debian.net/1254702/
But shellcheck complains about
egrep -o use grep -E instead. However if I do that any selection I make tries to open a local file. eg..https://i.imgur.com/M3OoBmf.jpg
Ignore shellcheck? I mean it works as I want, I think. Haven't tested spaces in denoscription much, which I'd very much like to have, but it does seem to work as is.
https://redd.it/xlbtgc
@r_bash
Bash Linkedin Skill Badge: Prepare for Skill Assessment and Get Badge on Your Profile
https://www.rbtechtips.com/linkedin-bash-assessment-answers-2022-bash-skill-badge/
https://redd.it/xlmkj3
@r_bash
https://www.rbtechtips.com/linkedin-bash-assessment-answers-2022-bash-skill-badge/
https://redd.it/xlmkj3
@r_bash
RBTechTips
LinkedIn Bash Assessment Answers 2022: Bash Skill Badge - RBTechTips
LinkedIn Bash Assessment Answers 2022: Bash Skill Badge: Bash LinkedIn Test Answers: In this post you will get all the correct answers of LinkedIn Bash quiz
how to validate options in noscript.
​
So I have noscript that takes file from user with option -y or -n and -d is required noscript runs ./example.sh -d file -y or -n variable. I made case getops "d:up" option`
​
Correct way running noscript: ./example.sh -d file -y or -n variable.
file="" -- that user inputs file
I need to do validation on if the user did not pass file in for example
./example.sh -y --- it should give error "must provide file"
./example.sh -d --- it should give error "must provide file"
./example.sh -y -n --- it should give error "must provide file"
i came up with this but its not working #checking if the file is provided
if $file -eq 1 ; then echo "No file provided"
exit 1
fi
# Does the file exist which works for checking if the file exists but it only check if the file in there
if [ ! -f $file ]; then
echo "Error: The file $file does not exist."
exit 1 fi
i already have validations for everything else but i need figure out how to make sure if the person doesnt provide file like examples above if gives out a error
https://redd.it/xm36mm
@r_bash
​
So I have noscript that takes file from user with option -y or -n and -d is required noscript runs ./example.sh -d file -y or -n variable. I made case getops "d:up" option`
​
Correct way running noscript: ./example.sh -d file -y or -n variable.
file="" -- that user inputs file
I need to do validation on if the user did not pass file in for example
./example.sh -y --- it should give error "must provide file"
./example.sh -n --- it should give error "must provide file" ./example.sh -d --- it should give error "must provide file"
./example.sh -y -n --- it should give error "must provide file"
i came up with this but its not working #checking if the file is provided
if $file -eq 1 ; then echo "No file provided"
exit 1
fi
# Does the file exist which works for checking if the file exists but it only check if the file in there
if [ ! -f $file ]; then
echo "Error: The file $file does not exist."
exit 1 fi
i already have validations for everything else but i need figure out how to make sure if the person doesnt provide file like examples above if gives out a error
https://redd.it/xm36mm
@r_bash
reddit
how to validate options in noscript.
So I have noscript that takes file from user with option -y or -n and -d is required noscript runs ./example.sh -d file -y or -n variable. I...
How to add all the values up?
So I want to add all the values up for each species, and get something like
adinocephala 1
atla 7
alicastrum 30
etc for my entire list, but I'm just a little lost on how to do it easily without having to grep each individual species and add up the values with a for loop. That would take absolutely forever and I know there's got to be an easier solution, but I'm not finding anything good on google.
https://preview.redd.it/ozyojuv8rqp91.png?width=258&format=png&auto=webp&s=11eeadfd510c2887ec4a12fe9a77ee19af42d8d0
Any help would be appreciated!!
https://redd.it/xmjy96
@r_bash
So I want to add all the values up for each species, and get something like
adinocephala 1
atla 7
alicastrum 30
etc for my entire list, but I'm just a little lost on how to do it easily without having to grep each individual species and add up the values with a for loop. That would take absolutely forever and I know there's got to be an easier solution, but I'm not finding anything good on google.
https://preview.redd.it/ozyojuv8rqp91.png?width=258&format=png&auto=webp&s=11eeadfd510c2887ec4a12fe9a77ee19af42d8d0
Any help would be appreciated!!
https://redd.it/xmjy96
@r_bash
Convert / compress all videos in folder
Hey guys,
i have a bunch of videos (197GB), mixed up as MKV, M4V, MP4, WEBM... different resolutions (1080, 720, ...).
My goal is to compress them all as a one-line format.
480p should be the final resolution, what could you recommend as file type/codec?
x265, webm, mp4... ?
It should be the best quality/best file size in the end :)
Thank you!
https://redd.it/xmq2uz
@r_bash
Hey guys,
i have a bunch of videos (197GB), mixed up as MKV, M4V, MP4, WEBM... different resolutions (1080, 720, ...).
My goal is to compress them all as a one-line format.
480p should be the final resolution, what could you recommend as file type/codec?
x265, webm, mp4... ?
It should be the best quality/best file size in the end :)
Thank you!
https://redd.it/xmq2uz
@r_bash
reddit
Convert / compress all videos in folder
Hey guys, i have a bunch of videos (197GB), mixed up as MKV, M4V, MP4, WEBM... different resolutions (1080, 720, ...). My goal is to compress...
!! in if statement
I can't seem to get this noscript working and I don't know how to google this one.
I want to run multiple commands, for every command I want to output that it failed without repeating the if statement over an over again, so I made the if statement at the beginning.
checklastcommand() {
if $? -eq 0 ; then
echo "!! OK"
else
echo "!! failed" >> ~/Desktop/testlog
fi
}
and i.E.
echo "Test"
checklastcommand
the output is always
!! OK
or
!! failed
instead of
echo "test" failed
https://redd.it/xmxreg
@r_bash
I can't seem to get this noscript working and I don't know how to google this one.
I want to run multiple commands, for every command I want to output that it failed without repeating the if statement over an over again, so I made the if statement at the beginning.
checklastcommand() {
if $? -eq 0 ; then
echo "!! OK"
else
echo "!! failed" >> ~/Desktop/testlog
fi
}
and i.E.
echo "Test"
checklastcommand
the output is always
!! OK
or
!! failed
instead of
echo "test" failed
https://redd.it/xmxreg
@r_bash
reddit
!! in if statement
I can't seem to get this noscript working and I don't know how to google this one. I want to run multiple commands, for every command I want to...
This vim/fzf alias in my bashrc isn't working, thoughts?
Hi ~
I need a alias to quickly type and open up a file in vim via fzf. I have gotten to the point where I can do this from the terminal via typing
// bashrc file
alias vimf="vim $(fzf)"
... then this results in some weird behavior where opening a new shell causes fzf to automatically come up. Also typing
What is the proper way here to create this alias?
https://redd.it/xn7sln
@r_bash
Hi ~
I need a alias to quickly type and open up a file in vim via fzf. I have gotten to the point where I can do this from the terminal via typing
vim $(fzf). However, if I add an alias like ... // bashrc file
alias vimf="vim $(fzf)"
... then this results in some weird behavior where opening a new shell causes fzf to automatically come up. Also typing
vimf has some really weird behavior as if it's opening up some cache'd file.What is the proper way here to create this alias?
https://redd.it/xn7sln
@r_bash
reddit
This vim/fzf alias in my bashrc isn't working, thoughts?
Hi ~ I need a alias to quickly type and open up a file in vim via fzf. I have gotten to the point where I can do this from the terminal via...
Help required improving an SFTP RPA noscript
Hey Folks,
Newbie Bash Scriptor here, I was able to collate a SFTP RPA for our setup. However there seems to be two issues that I have detected with the logic for which I have been unable to find better solutions for
1. First is the timestamp variable - It is updating the file metadata for tracking the latest files added to the folder, the issue that I have noticed(edge case)is if multiple files are written in the same minute marker as the noscript is running, it seems to miss out on files, I think tracking it to even keep track for seconds should be fine. (Rsync would have been better but the remote site actually moves the file away on a per second basis)
2. Verbosity of the SFTP piece - Is there a way to write the SFTP output to the file as well, right now it is only able to redirect the output of the noscript but not the SFTP module, the SFTP module showcases the progress bars that I want to feed into my list but have been unable to do so far.
​
#!/bin/bash
#
# Use batch file with SFTP shell noscript
#
##################################################################
# Create SFTP batch file
tempfile=$(mktemp -t sftptmp.XXXX)
count=0
archive="/SFTP/ARCHIVE/"
currenttime=$(date)
trap "/bin/rm -f $tempfile" 0 1 15
if [ $# -eq 0 ] ; then
echo "Usage: $0 user host path_to_src_dir remote_dir" >&2
exit 1
fi
# Collect User Input
user="$1"
server="$2"
remote_dir="$4"
source_dir="$3"
identity="$5"
timestamp="$source_dir/.timestamp"
# Without source and remote dir, the noscript cannot be executed
if [[ -z $remote_dir ]] || [[ -z $source_dir ]]; then
echo "Provide source and remote directory both"
exit 1
fi
echo $currenttime
echo "cd $remote_dir" >> $tempfile
# timestamp file will not be available when executed for the very first time
if [ ! -f $timestamp ] ; then
# no timestamp file, upload all files
for filename in $source_dir/*
do
if [ -f "$filename" ] ; then
# Place the command to upload files in sftp batch file
echo "put -P \"$filename\"" | tee -a $tempfile
# Increase the count value for every file found
count=$(( $count + 1 ))
fi
done
else
# If timestamp file found then it means it is not the first execution so look out for newer files only
# Check for newer files based on the timestamp
for filename in $(find $source_dir -newer $timestamp -type f -print)
do
# If found newer files place the command to upload these files in batch file
echo "put -P \"$filename\"" | tee -a $tempfile
# Increase the count based on the new files
count=$(( $count + 1 ))
done
fi
# If no new files found the do nothing
if [ $count -eq 0 ] ; then
echo "$0: No files require uploading to $server" | tee -a /var/log/rpa
exit 1
fi
# Place the command to exit the sftp connection in batch file
echo "quit" | tee -a $tempfile
echo $tempfile
echo "Synchronizing: Found $count files in local folder to upload."
# Main command to use batch file with SFTP shell noscript without prompting password
sftp -i $identity -P 10022 -b $tempfile "$user@$server" | tee -a /var/log/rpa
echo "Done. All files synchronized up with $server"
# Create timestamp file once first set of files are uploaded
touch $timestamp
# Remove the sftp batch file
rm -f $tempfile
exit 0
https://redd.it/xnzqvu
@r_bash
Hey Folks,
Newbie Bash Scriptor here, I was able to collate a SFTP RPA for our setup. However there seems to be two issues that I have detected with the logic for which I have been unable to find better solutions for
1. First is the timestamp variable - It is updating the file metadata for tracking the latest files added to the folder, the issue that I have noticed(edge case)is if multiple files are written in the same minute marker as the noscript is running, it seems to miss out on files, I think tracking it to even keep track for seconds should be fine. (Rsync would have been better but the remote site actually moves the file away on a per second basis)
2. Verbosity of the SFTP piece - Is there a way to write the SFTP output to the file as well, right now it is only able to redirect the output of the noscript but not the SFTP module, the SFTP module showcases the progress bars that I want to feed into my list but have been unable to do so far.
​
#!/bin/bash
#
# Use batch file with SFTP shell noscript
#
##################################################################
# Create SFTP batch file
tempfile=$(mktemp -t sftptmp.XXXX)
count=0
archive="/SFTP/ARCHIVE/"
currenttime=$(date)
trap "/bin/rm -f $tempfile" 0 1 15
if [ $# -eq 0 ] ; then
echo "Usage: $0 user host path_to_src_dir remote_dir" >&2
exit 1
fi
# Collect User Input
user="$1"
server="$2"
remote_dir="$4"
source_dir="$3"
identity="$5"
timestamp="$source_dir/.timestamp"
# Without source and remote dir, the noscript cannot be executed
if [[ -z $remote_dir ]] || [[ -z $source_dir ]]; then
echo "Provide source and remote directory both"
exit 1
fi
echo $currenttime
echo "cd $remote_dir" >> $tempfile
# timestamp file will not be available when executed for the very first time
if [ ! -f $timestamp ] ; then
# no timestamp file, upload all files
for filename in $source_dir/*
do
if [ -f "$filename" ] ; then
# Place the command to upload files in sftp batch file
echo "put -P \"$filename\"" | tee -a $tempfile
# Increase the count value for every file found
count=$(( $count + 1 ))
fi
done
else
# If timestamp file found then it means it is not the first execution so look out for newer files only
# Check for newer files based on the timestamp
for filename in $(find $source_dir -newer $timestamp -type f -print)
do
# If found newer files place the command to upload these files in batch file
echo "put -P \"$filename\"" | tee -a $tempfile
# Increase the count based on the new files
count=$(( $count + 1 ))
done
fi
# If no new files found the do nothing
if [ $count -eq 0 ] ; then
echo "$0: No files require uploading to $server" | tee -a /var/log/rpa
exit 1
fi
# Place the command to exit the sftp connection in batch file
echo "quit" | tee -a $tempfile
echo $tempfile
echo "Synchronizing: Found $count files in local folder to upload."
# Main command to use batch file with SFTP shell noscript without prompting password
sftp -i $identity -P 10022 -b $tempfile "$user@$server" | tee -a /var/log/rpa
echo "Done. All files synchronized up with $server"
# Create timestamp file once first set of files are uploaded
touch $timestamp
# Remove the sftp batch file
rm -f $tempfile
exit 0
https://redd.it/xnzqvu
@r_bash
reddit
Help required improving an SFTP RPA noscript
Hey Folks, Newbie Bash Scriptor here, I was able to collate a SFTP RPA for our setup. However there seems to be two issues that I have detected...
Vertical to Horizontal Output in Bash ?
Hi,
I have a command giving the following output which is stored as-is in a text file and then displayed on screen:
Connected to 172.16.1.6.
Port Power(dBm)
------------------
0 31.10
1 31.06
2 27.75
3 28.60
Connected to 172.16.1.7.
Port Power(dBm)
------------------
0 30.51
1 30.92
2 30.06
3 28.85
Connected to 172.16.1.8.
Port Power(dBm)
------------------
0 30.95
1 30.29
2 30.18
3 32.07
How can I format this output into a table with 3 columns horizontally as follows:
Connected to 172.16.1.6. | Connected to 172.16.1.7.| Connected to 172.16.1.8.
Port Power(dBm) | Port Power(dBm) | Port Power(dBm)
------------------ | ------------------ | ------------------
0 31.10 | 0 30.51 | 0 30.95
1 31.06 | 1 30.92 | 1 30.29
2 27.75 | 2 30.06 | 2 30.18
3 28.60 | 3 28.85 | 3 32.07
Thanks..
https://redd.it/xod9s0
@r_bash
Hi,
I have a command giving the following output which is stored as-is in a text file and then displayed on screen:
Connected to 172.16.1.6.
Port Power(dBm)
------------------
0 31.10
1 31.06
2 27.75
3 28.60
Connected to 172.16.1.7.
Port Power(dBm)
------------------
0 30.51
1 30.92
2 30.06
3 28.85
Connected to 172.16.1.8.
Port Power(dBm)
------------------
0 30.95
1 30.29
2 30.18
3 32.07
How can I format this output into a table with 3 columns horizontally as follows:
Connected to 172.16.1.6. | Connected to 172.16.1.7.| Connected to 172.16.1.8.
Port Power(dBm) | Port Power(dBm) | Port Power(dBm)
------------------ | ------------------ | ------------------
0 31.10 | 0 30.51 | 0 30.95
1 31.06 | 1 30.92 | 1 30.29
2 27.75 | 2 30.06 | 2 30.18
3 28.60 | 3 28.85 | 3 32.07
Thanks..
https://redd.it/xod9s0
@r_bash
reddit
Vertical to Horizontal Output in Bash ?
Hi, I have a command giving the following output which is stored as-is in a text file and then displayed on screen: Connected to...
Getting a substring in PS1?
Could anybody give me some hits as how to elegantly get a substring some elements in my prompt?
For instance I'd like just the first letter of the username and host:
Host: Bar
User: Foo
F@B:~$
Do I have to stick a full cut or awk command in PS1 or is there a slick way to get a substring of \u and \h ?
Thanks
https://redd.it/xofz2z
@r_bash
Could anybody give me some hits as how to elegantly get a substring some elements in my prompt?
For instance I'd like just the first letter of the username and host:
Host: Bar
User: Foo
F@B:~$
Do I have to stick a full cut or awk command in PS1 or is there a slick way to get a substring of \u and \h ?
Thanks
https://redd.it/xofz2z
@r_bash
reddit
Getting a substring in PS1?
Could anybody give me some hits as how to elegantly get a substring some elements in my prompt? For instance I'd like just the first letter of...