关于双击图标无法打开shell脚本、*.out 文件

本文探讨了Shell脚本执行时遇到的问题,即脚本未能按预期的方式运行。作者通过实验发现,脚本的实际执行路径并非其所在路径,而是当前用户的家目录。文章进一步讨论了如何正确地指定路径以避免此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

今天写了一个shell脚本,双击却不能执行,然后换成*.out 也不行。

仔细研究发现:其实他们都执行了,怎么知道它们都执行了呢?我让它们像一个文件中写入数据。

但是为什么没有我们想要的运行结果呢?这取决于程序的功能,如果在程序里面用到了目录操作,就有可能会出错了,

 

我的shell脚本放在/home/jim/lex_yacc/

在脚本中加入这样一行"pwd > log.txt"

结果log.txt中的内容却为/home/jim

如果是root用户登陆的话log.txt中的内容则是/root

 

这说明 它们的 执行目录并不是 脚本(应用程序)所在的目录,

所以脚本或是程序中一定要指明具体的目录而不能用 " ./ ../  " 之类

如果使用同一个目录多次,就可以定义一个变量,在这里

MYPATH=~/lex_yacc

mkdir $MYPATH/mydir

 

这样就行了

为什么我写什么 小蓝窗都没反应“PS C:\Users\Administrator> # 1. 修复错误的快捷方式 >> $desktopPath = [Environment]::GetFolderPath("Desktop") >> $badShortcut = Join-Path $desktopPath "PS7_Direct.lnk" >> >> if (Test-Path $badShortcut) { >> try { >> # 检查快捷方式目标 >> $shell = New-Object -ComObject WScript.Shell >> $shortcut = $shell.CreateShortcut($badShortcut) >> >> if ($shortcut.TargetPath -notlike "*pwsh.exe") { >> # 创建正确的快捷方式 >> $correctShortcut = Join-Path $desktopPath "PS7_Correct.lnk" >> $pwshPath = (Get-Command pwsh).Source >> >> $newShortcut = $shell.CreateShortcut($correctShortcut) >> $newShortcut.TargetPath = $pwshPath >> $newShortcut.Arguments = "-NoExit" >> $newShortcut.IconLocation = "$pwshPath,0" >> $newShortcut.WorkingDirectory = $desktopPath >> $newShortcut.Save() >> >> # 替换错误快捷方式 >> Remove-Item $badShortcut -Force >> Rename-Item $correctShortcut "PS7.lnk" >> >> Write-Host "已修复快捷方式: PS7.lnk" -ForegroundColor Green >> } >> } >> catch { >> Write-Host "快捷方式修复失败: $($_.Exception.Message)" -ForegroundColor Red >> } >> } >> >> # 2. 修复配置文件路径问题 >> $ps7ProfileDir = Join-Path ([Environment]::GetFolderPath('MyDocuments')) "PowerShell" >> $ps7ProfilePath = Join-Path $ps7ProfileDir "Microsoft.PowerShell_profile.ps1" >> >> $winPSProfileDir = Join-Path ([Environment]::GetFolderPath('MyDocuments')) "WindowsPowerShell" >> $winPSProfilePath = Join-Path $winPSProfileDir "Microsoft.PowerShell_profile.ps1" >> >> # 确保正确的配置文件存在 >> if (-not (Test-Path $ps7ProfilePath)) { >> # 从Windows PowerShell位置复制配置文件 >> if (Test-Path $winPSProfilePath) { >> $null = New-Item -ItemType Directory -Path $ps7ProfileDir -Force >> Copy-Item $winPSProfilePath $ps7ProfilePath -Force >> Write-Host "已复制配置文件到PowerShell 7位置" -ForegroundColor Cyan >> } >> else { >> # 重新创建配置文件 >> $profileContent = @' >> # PowerShell 7 基础配置 >> Write-Host "✅ PowerShell $($PSVersionTable.PSVersion) 已加载" -ForegroundColor Green >> Set-Alias ll Get-ChildItem >> '@ >> $profileContent | Out-File $ps7ProfilePath -Encoding UTF8 >> Write-Host "已创建基础配置文件" -ForegroundColor Cyan >> } >> } >> >> # 3. 添加版本检测和自动修复 >> $autoFixScript = @' >> # 自动环境修复脚本 >> function Repair-PS7Environment { >> # 检查PowerShell版本 >> if ($PSVersionTable.PSVersion.Major -lt 7) { >> Write-Host "⚠️ 当前运行的是PowerShell $($PSVersionTable.PSVersion)" -ForegroundColor Red >> Write-Host "正在尝试启动PowerShell 7..." -ForegroundColor Yellow >> >> try { >> # 尝试通过快捷方式启动 >> $desktop = [Environment]::GetFolderPath("Desktop") >> $shortcut = Join-Path $desktop "PS7.lnk" >> >> if (Test-Path $shortcut) { >> $shell = New-Object -ComObject WScript.Shell >> $shell.Run($shortcut) >> } >> else { >> # 直接启动pwsh >> Start-Process pwsh -ArgumentList "-NoExit" >> } >> >> # 退出当前会话 >> exit >> } >> catch { >> Write-Host "启动失败: $($_.Exception.Message)" -ForegroundColor Red >> } >> } >> >> # 检查配置文件加载 >> if (-not (Test-Path $PROFILE)) { >> Write-Host "⚠️ 配置文件未找到" -ForegroundColor Yellow >> Write-Host "正在创建基础配置文件..." -ForegroundColor Cyan >> >> @' >> # PowerShell 7 基础配置 >> Write-Host "✅ PowerShell $($PSVersionTable.PSVersion) 已加载" -ForegroundColor Green >> Set-Alias ll Get-ChildItem >> '@ | Out-File $PROFILE -Encoding UTF8 >> } >> } >> >> # 启动时自动运行修复 >> Repair-PS7Environment >> '@ >> >> # 将修复脚本添加到配置文件 >> $currentProfile = if (Test-Path $ps7ProfilePath) { >> Get-Content $ps7ProfilePath -Raw >> } else { "" } >> >> if (-not $currentProfile.Contains("Repair-PS7Environment")) { >> $autoFixScript + "`n`n" + $currentProfile | Out-File $ps7ProfilePath -Encoding UTF8 >> Write-Host "已添加自动修复功能到配置文件" -ForegroundColor Green >> } >> >> # 4. 创建验证脚本 >> $verificationScript = @' >> # 环境验证脚本 >> Write-Host "`n=== PowerShell 7 环境验证 ===" -ForegroundColor Cyan >> >> # 1. 版本验证 >> Write-Host "PowerShell 版本: $($PSVersionTable.PSVersion)" >> if ($PSVersionTable.PSVersion.Major -ge 7) { >> Write-Host "✅ 版本验证通过" -ForegroundColor Green >> } else { >> Write-Host "❌ 需要PowerShell 7或更高版本" -ForegroundColor Red >> } >> >> # 2. 配置文件验证 >> Write-Host "`n配置文件状态: " -NoNewline >> if (Test-Path $PROFILE) { >> Write-Host "已加载" -ForegroundColor Green >> Write-Host "配置文件路径: $PROFILE" >> } else { >> Write-Host "未找到" -ForegroundColor Red >> } >> >> # 3. 快捷方式验证 >> Write-Host "`n桌面快捷方式验证:" >> $desktop = [Environment]::GetFolderPath("Desktop") >> $shortcuts = Get-ChildItem $desktop -Filter "PS7*.lnk" >> >> if ($shortcuts) { >> foreach ($shortcut in $shortcuts) { >> $shell = New-Object -ComObject WScript.Shell >> $sc = $shell.CreateShortcut($shortcut.FullName) >> >> $status = if ($sc.TargetPath -like "*pwsh.exe") { >> "✅ 有效" >> } else { >> "❌ 无效" >> } >> >> Write-Host " - $($shortcut.Name): $status" -ForegroundColor $( >> if ($status -eq "✅ 有效") { "Green" } else { "Red" } >> ) >> Write-Host " 目标: $($sc.TargetPath)" >> Write-Host " 参数: $($sc.Arguments)" >> } >> } else { >> Write-Host " - 未找到PS7快捷方式" -ForegroundColor Yellow >> } >> >> # 4. 基本功能测试 >> Write-Host "`n基本功能测试:" >> try { >> $result = Invoke-RestMethod -Uri "https://httpbin.org/get" -TimeoutSec 5 >> Write-Host " - HTTP测试: 成功" -ForegroundColor Green >> } catch { >> Write-Host " - HTTP测试: 失败 - $($_.Exception.Message)" -ForegroundColor Red >> } >> >> try { >> $json = '{"test": true}' | ConvertFrom-Json >> Write-Host " - JSON测试: 成功" -ForegroundColor Green >> } catch { >> Write-Host " - JSON测试: 失败" -ForegroundColor Red >> } >> >> Write-Host "`n验证完成!" -ForegroundColor Cyan >> '@ >> >> # 保存验证脚本 >> $verifyPath = Join-Path $env:TEMP "Verify-PS7Environment.ps1" >> $verificationScript | Out-File $verifyPath -Encoding UTF8 >> >> # 5. 启动验证 >> Write-Host "`n正在启动环境验证..." -ForegroundColor Cyan >> Start-Process pwsh -ArgumentList "-NoExit", "-File `"$verifyPath`"" >> >> # 检查PowerShell版本 >> $PSVersionTable.PSVersion >> >> # 检查配置文件 >> Test-Path $PROFILE >> >> # 检查快捷方式目标 >> $shell = New-Object -ComObject WScript.Shell >> $shortcut = $shell.CreateShortcut("C:\Users\Administrator\Desktop\PS7.lnk") >> $shortcut.TargetPath >> >> # 直接运行验证脚本 >> & "$env:TEMP\Verify-PS7Environment.ps1" >> >> # 创建一键修复快捷方式 >> $desktop = [Environment]::GetFolderPath("Desktop") >> $shortcutPath = Join-Path $desktop "Fix_PS7.lnk" >> $shell = New-Object -ComObject WScript.Shell >> $shortcut = $shell.CreateShortcut($shortcutPath) >> $shortcut.TargetPath = "pwsh.exe" >> $shortcut.Arguments = "-Command `"& {Start-Process pwsh -ArgumentList '-NoExit','-File',`"$verifyPath`"}`"" >> $shortcut.IconLocation = "C:\Windows\System32\shell32.dll,2" >> $shortcut.Save() >> >> # 每月自动验证环境 >> $trigger = New-JobTrigger -Daily -At "9:00 AM" -DaysInterval 30 >> $action = { >> & "$env:TEMP\Verify-PS7Environment.ps1" | Out-File "$env:TEMP\PS7_HealthCheck.log" >> } >> Register-ScheduledJob -Name "PS7_Maintenance" -ScriptBlock $action -Trigger $trigger >>”
最新发布
08-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值