Rewrite Python script on Bash: import os prevuser = '' for v in os.environ: vr =…
- User
Rewrite Python script on Bash:
import os
prev_user = '' for v in os.environ: vr = os.environ[v] if '/home/' in vr: prev_user = vr.split('/')[2].strip() break
print('Changed from', prev_user) for v in os.environ: vr = os.environ[v].replace(prev_user, os.environ['USER']) os.system(f'export {v}="{vr}"')
- Assistant
You can achieve a similar effect in Bash with the following script. This script will iterate over the environment variables, find the path that contains '/home/', extract the previous user, and then update it with the current user.
Here's the Bash equivalent of the provided Python script:
#!/bin/bash prev_user="" # Loop through the environment variables for v in $(env | grep '/home/'); do # Get the variable name and value varname=$(echo "$v" | cut -d= -f1) varvalue=$(echo "$v" | cut -d= -f2-) # Extract the previous user from the value prev_user=$(echo "$varvalue" | grep -oP '/home/\K[^/]+') # Break if we found the user if [[ -n "$prev_user" ]]; then break fi done echo "Changed from $prev_user" # Update the environment variables for v in $(env); do varname=$(echo "$v" | cut -d= -f1) varvalue=$(echo "$v" | cut -d= -f2-) # Replace the previous user with the current user new_value="${varvalue//$prev_user/$USER}" # Export the updated variable (Note: this change will only affect this session) export "$varname=$new_value" done - User
./su-fix-envars.sh: line 31: export: `autofitter:no-stem-darkening=0': not a valid identifier
- Assistant
The error you're encountering stems from the fact that the variable name in the export command must adhere to the naming rules of shell variables, which means it cannot contain characters like
:or-. These characters are not valid for environment variable names.